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:");

5
  • array2 does not have a property of id? Did you mean to compare against an item in the equivalent index in array2? Commented Dec 1, 2021 at 9:44
  • please add the wanted result. what should happen if array1 has another object, not matching in array2? Commented Dec 1, 2021 at 9:45
  • How to judge it is a duplicate? Just id? Commented Dec 1, 2021 at 9:45
  • @mouxiaochou yes id Commented Dec 1, 2021 at 9:51
  • @NinaScholz I put the expected output, please check Commented Dec 1, 2021 at 9:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 see edit in the answer, you can create a function to accept your arrays as input
3

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

7 Comments

The duplicate item does not tell me which one it is
i edit my reply you can have them with another variable
I tested your code, it gives me an empty array, please check, thank you
i edit my reply to have correct answer
we can't use array1.includes(valueFromArray2) because includes check if object are equal in memory and not have same shape stackoverflow.com/questions/49187940/…
|
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; }

1 Comment

He wanted the 1,2,3,4 in the first array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.