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;
}

JavaScript finding intersection values from the 2 arrays

function arrayIntersection(ar1, ar2) {
return ar1.filter(function(n) {
return ar2.indexOf(n) !== -1;
});
}

JavaScript function to remove duplicates in single dimension array

function removeDuplicatesInSingleDimensionArray(array) {
  return array.filter((a, b) => array.indexOf(a) === b)
};