สมมติอาร์เรย์ของเราที่มีองค์ประกอบนำหน้าด้วยเครื่องหมาย + คือ
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' ]