2

I have two arrays, I want these two to be combined and duplicate items are not lit and only new items are added and I see the combined array in the console I also want to see the duplicate item in the list on the console I mean, I need two outputs, one to tell me what a duplicate is, and one to show me a new array of combinations of two arrays without duplicates. I wrote this code but it does not work properly Thanks for guiding me

Expected output:

newContact : [
  {
    "id": 1,
    "name": "Oliver"
  },
  {
    "id": 2,
    "name": "Liam"
  }
   {
    "id": 3,
    "name": "James"
  }
   {
    "id": 4,
    "name": "Lucas"
  }
]
duplicateContacts: Oliver

let array1 = [
  { id: 1, name: "Oliver" },
  { id: 2, name: "Liam" },
];
let array2 = [
  { id: 1, name: "Oliver" },
  { id: 3, name: "James" },
  { id: 4, name: "Lucas" },
];
let newContact = array1.filter((item) => item.id !== array2.id);
console.log("newContact :", newContact);
console.log("duplicateContacts:");
Good
  • 81
  • 1
  • 7

3 Answers3

4

Guess duplicate meaning the same id, add array entries to a map to find duplicates, and the map contains unique items

const array1 = [
  { id: 1, name: "Oliver" },
  { id: 2, name: "Liam" },
];
const array2 = [
  { id: 1, name: "Oliver" },
  { id: 3, name: "James" },
  { id: 4, name: "Lucas" },
];

const duplicates = [];
const map = new Map();

const fill = (array) => array.forEach(item => {
  if (map.has(item.id)) {
    duplicates.push(item);
  } else {
    map.set(item.id, item);
  }
});

fill(array1);
fill(array2);

console.log(duplicates);
console.log([...map.values()]);

Edit

To take arrays as input, create a function

const inspect = (...arrays) => {
    const duplicates = [];
    const map = new Map();

    const fill = (array) => array.forEach(item => {
      if (map.has(item.id)) {
        duplicates.push(item);
      } else {
        map.set(item.id, item);
      }
    });

    arrays.forEach(array => fill(array));

    console.log(duplicates);
    console.log([...map.values()]);
};


const array1 = [
  { id: 1, name: "Oliver" },
  { id: 2, name: "Liam" },
];
const array2 = [
  { id: 1, name: "Oliver" },
  { id: 3, name: "James" },
  { id: 4, name: "Lucas" },
];

inspect(array1, array2);
ProDec
  • 5,390
  • 1
  • 3
  • 12
  • Your code does what I expect, but can the `fill` function not be called twice and no need to be called? And give it the input ourselves – Good Dec 01 '21 at 10:09
  • @Good see edit in the answer, you can create a function to accept your arrays as input – ProDec Dec 01 '21 at 10:14
2

with three methods :

  • filter to get an array of duplicate element

  • map to recover only an array of one column

  • some to describe how object in array should be compared

     const duplicates = array1.filter(value => array2.some(oneElement => oneElement.id === value.id));
    const duplicatesId = array1.filter(value => array2.some(oneElement => oneElement.id === value.id)).map(oneElement => oneElement.id);
    
    
    const mergedArray = [ 
     ...array1, 
     ...array2.filter(value => !duplicatesId.some(oneDuplicate => oneDuplicate === value.id))
    ];
    

const array1 = [
  { id: 1, name: "Oliver" },
  { id: 2, name: "Liam" },
];

const array2 = [
  { id: 1, name: "Oliver" },
  { id: 3, name: "James" },
  { id: 4, name: "Lucas" },
];

const duplicates = array1.filter(value => array2.some(oneElement => oneElement.id === value.id));
const duplicatesId = array1.filter(value => array2.some(oneElement => oneElement.id === value.id)).map(oneElement => oneElement.id);


const mergedArray = [ 
  ...array1, 
  ...array2.filter(value => !duplicatesId.some(oneDuplicate => oneDuplicate === value.id))
];

console.log(duplicates);
console.log(duplicatesId);
console.log(mergedArray);
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
2

You could take a Set from array2with all id and filter the other array.

const
    array1 = [{ id: 1, name: "Oliver" }, { id: 2, name: "Liam" }],
    array2 = [{ id: 1, name: "Oliver" }, { id: 3, name: "James" }, { id: 4, name: "Lucas" }],
    set2 = new Set(array2.map(({ id }) => id)),
    duplicateContacts = array1.filter(({ id }) => set2.has(id));
    allContacts = [...array1, ...array2].filter(
        (s => ({ id }) => !s.has(id) && s.add(id))
        (new Set)
    ),

console.log("allContacts:", allContacts);
console.log("duplicateContacts:", duplicateContacts);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392