indexOf array deduplication method - JavaScript

Javascript array of deduplication method -indexof

Array deduplication: Duplicate values stored in the array
if the array is [1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4, 4,4,4,5,5,5,5,5]
deduplication results should be: [1,2,3,4,5]

Idea:
to create a new array, the value of the original value, the new array is written
if the value does not exist in the new array, on the implementation of written, if it already exists, not written

var arr = [1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,5,5,5,5]
 
var newArr = [];

// 循环遍历,获取原始数组arr中的所有数值
arr.forEach(function(v){

     // 在新数组中,查找当前获取的原始数组的数值
     // newArr.indexOf(v) 执行结果如果是 -1
     // 证明在新数组中,没有这个原始数组的数据
     if(newArr.indexOf(v) === -1){
         // 将这个数据,写入到新数组中
         newArr.push(v)
     }
 })

  console.log( newArr );
Released five original articles · won praise 1 · views 105

Guess you like

Origin blog.csdn.net/abc6507/article/details/105015965