在包含多个值JavaScript的数组中移除相同值
假设以下数组包含相似的值 -
const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];
为了从数组中移除相似的值,使用 set() 的概念。以下为代码 -
示例
const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];
console.log("The value="+listOfStudentName);
const doesNotContainSameElementTwice = [...new Set(listOfStudentName)];
console.log("The Array=");
console.log(doesNotContainSameElementTwice)要运行以上程序,你需要使用以下指令 -
node fileName.js.
这里,我的文件名是 demo42.js。
输出
这将会产生以下输出 -
PS C:\Users\Amit\JavaScript-code> node demo42.js The value=John,Mike,John,Bob,Mike,Sam,Bob,John The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP