JavaScript 从数组中删除所有以 + 号开头的元素的 '+'

javascriptweb developmentobject oriented programming更新于 2025/10/29 21:00:17

假设以下是我们的数组,其中元素以 + 号开头 −

var studentNames =
[
   '+John Smith',
   '+David Miller',
   '+Carol Taylor',
   '+John Doe',
   '+Adam Smith'
];

要删除 + 号,代码如下 −

示例

studentNames =
[
   '+John Smith',
   '+David Miller',
   '+Carol Taylor',
   '+John Doe',
   '+Adam Smith'
];
console.log("The actual array=");
console.log(studentNames);
studentNames = studentNames.map(function (value) {
   return value.replace('+', '');
});
console.log("After removing the + symbol, The result is=");
console.log(studentNames);

要运行上述程序,您需要使用以下命令 −

node fileName.js.

这里的文件名是 demo205.js。

输出

这将产生以下输出 −

PS C:\Users\Amit\javascript-code> node demo205.js
The actual array=
[
   '+John Smith',
   '+David Miller',
   '+Carol Taylor',
   '+John Doe',
   '+Adam Smith'
]
After removing the + symbol, The result is=
[
   'John Smith',
   'David Miller',
   'Carol Taylor',
   'John Doe',
   'Adam Smith'
]

相关文章


有用资源