JavaScript
1. splice
let arr = [1, 2, 3, 5, 6, 4, 3, 2, 1, 1, 2, 3, 4, 5] for(let i = 0; i < arr.length - 1; ++i) { for(let j = i + 1; j < arr.length; ++j) { if(arr[i] === arr[j]) { arr.splice(j--, 1) } } } console.log(arr)
2. new Array
let arr = [1, 2, 3, 5, 6, 4, 3, 2, 1, 1, 2, 3, 4, 5] function dedupe(array) { let interim = [] array.forEach(function(value) { // if(this.indexOf(value) === -1) if(!this.includes(value)) interim.push(value) }, interim) return interim } console.log(dedupe(arr))
3. Object[key] can't difference 5 '5'
let arr = [1, 2, 3, 5, 6, 4, 3, 2, 1, 1, 2, 3, 4, 5, '5'] function dedupe(array) { let interim = {} array.forEach(function(value) { interim[value] = null }) return Object.keys(interim).map(v => parseInt(v)) } console.log(dedupe(arr))
标签:arr,Programming,value,interim,let,elimination,array,dedupe From: https://www.cnblogs.com/dissipate/p/17455428.html