Tuesday 3 March 2020

JavaScript: remove duplicate in multidimensional array by using specific key

     function removeDuplicatesMulti(array) {
// Declare a new array
var newArray = [];
 
// Declare an empty object
var uniqueObject = {};
 
// Loop for the array elements
for (var i in array) {
// Extract the title or key based on duplicate finding
objTitle = array[i]['ID'];
 
// Use the title as the index
uniqueObject[objTitle] = netSerices[i];
}
 
// Loop to push unique object into array
for (i in uniqueObject) {
newArray.push(uniqueObject[i]);
}

return newArray;
}

No comments:

Post a Comment