Deduplication Array, an array of objects to heavy

 
Deduplication Array:
 
function unique2 (array) {// Array deduplication
var n = []; // a new temporary array
// traverse the current array
for(var i = 0; i < array.length; i++){
// If the current array of i saved into a temporary array, skip,
// Otherwise the current term push to a temporary array inside
if (n.indexOf(array[i]) == -1) {
n.push(array[i]);
}
}
return n;
}
let result1 = unique2(ary);
console.log(result1)
 
 
Array of objects to re-:
var arr = [{name: 'zheng'}, {name: 'hai'}, {name: 'xin'}, {name: 'zheng'}, {name: 'zheng'}, {name: 'hai' }, {name: 'xin'}, {name: 'zheng'}];
function unique (array) {// Object deduplication method
var allArr = []; // Create a new temporary array
for(var i=0;i<array.length;i++){
was flag = true;
  for(var j=0;j<allArr.length;j++){
if(array[i].name== allArr[j].name){
      flag = false;
   };
};
if(flag){
  allArr.push (array [i]); // iterate arry When the flag is true when put into a temporary array, when the flag is false when the temporary hold this array and the array to re just a theory, but you want to add flag is a flag placed in the array to achieve the effect of weight
};
};
return allArr;
}
var result = unique1(arr);

Guess you like

Origin www.cnblogs.com/zhx119/p/11238979.html