Answers for "javascript merge arrays of objects without duplicates"

1

javascript merge arrays of objects without duplicates

var merged = [...initialData, ...newData.filter(d => !ids.has(d.ID))];
Posted by: Guest on February-19-2021
0

javascript merge two arrays of objects without duplicates

var initialData = [{
    'ID': 1,
    'FirstName': 'Sally'
  },
  {
    'ID': 2,
    'FirstName': 'Jim'
  },
  {
    'ID': 3,
    'FirstName': 'Bob'
  }
];

var newData = [{
    'ID': 2,
    'FirstName': 'Jim'
  },
  {
    'ID': 4,
    'FirstName': 'Tom'
  },
  {
    'ID': 5,
    'FirstName': 'George'
  }
];

var ids = new Set(initialData.map(d => d.ID));
var merged = [...initialData, ...newData.filter(d => !ids.has(d.ID))];
Posted by: Guest on July-09-2021
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

Code answers related to "javascript merge arrays of objects without duplicates"

Code answers related to "Javascript"

Browse Popular Code Answers by Language