JavaScript 從每個元素之前都有 + 號的陣列中刪除所有 +


假設我們的陣列中每個元素前面都有一個 + 號,如下 −

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'
]

更新於: 01-Sep-2020

122 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.