Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

JavaScript: Loại bỏ tất cả dấu '+' từ mảng mà mỗi phần tử đều có dấu + ở đầu


Giả sử mảng có phần tử bắt đầu bằng dấu +:

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

Để loại bỏ dấu +, dùng map() và replace():

Ví dụ

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);

Chạy: node fileName.js. File demo205.js.

Kết quả

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