Answers for "using set to remove duplicates objects inside the array javascript"

0

combine 2 "arrays with objects" and remove object duplicates javascript

// Join Without Dupes.
const joinWithoutDupes = (A, B) => {
  const a = new Set(A.map(x => x.item))
  const b = new Set(B.map(x => x.item))
  return [...A.filter(x => !b.has(x.item)), ...B.filter(x => !a.has(x.item))]
}

// Proof.
const output = joinWithoutDupes([{item:"apple",description: "lorem"},{item:"peach",description: "impsum"}], [{item:"apple", description: "dolor"},{item:"grape", description: "enum"}])
console.log(output)
Posted by: Guest on January-21-2021
-1

remove duplicates objects from array javascript

function remove_duplicate_objects(data,prop) {
    var seen = {};
    data = data.filter(function (entry) {
      if (seen.hasOwnProperty(entry[prop])) {
        return false;
      }

      seen[entry.prop] = entry;
      return true;
    });
    return data
  }

const new_array = remove_duplicate_objects([array with objects inside])
Posted by: Guest on February-23-2021

Code answers related to "using set to remove duplicates objects inside the array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language