-1

I have two array of objects (array1, array2). I am trying return final array(as shown below) which eliminates duplicates from array2 but not array1. I am giving priority to array1.

array1 =[
    { phone: "07485454", name: "John" },
    { phone: "054554", name: "Ryan" },
]

array2 =[
    { phone: "2144564", name: "John" },
    { phone: "286456", name: "Mike" },
]

This is something I want as a final result. Remove duplicates from array2 only. Final Array:

[
   { phone: "07485454", name: "John" },
   { phone: "054554", name: "Ryan" },
   { phone: "286456", name: "Mike" },
]

This is something that I have tried:

for(let i = 0; i < array1.length; i++) {
    let name = array1[i].name;
  for(let a = 0; i < array2.length; a++) {
      let newname = array2[a].name;
      if(name !== newname) {
       array1.push(
         {
           phone: array2[a].phone,
           name: array2[a].name
         });
      }
    
    console.log(array1);
  }
  
}

This is the error I get.

"errorType": "TypeError",
  "errorMessage": "Cannot read property 'name' of undefined",
  • 3
    Hi, and first of all: Welcome to Stackoverflow! It would be really helpful and great if you worte a little bit more about the things you already tried out: What didn't work, what errors you encountered, where did you get stuck? Right now, this questions seems like you are requesting someone else to do the implementing work for you which is not really the purpose of stack-overflow. – David Losert Jul 31 '20 at 10:17
  • @DavidLosert Apologies rookie mistake –  Jul 31 '20 at 11:00
  • @DalalV you have a mild typo in your inner for `for(let a = 0; i < array2.length; a++) {`. It should use be `a < array2.length`. Separately you have a couple of logical errors, try updating your code and debugging what's going on! – Khez Jul 31 '20 at 12:51

2 Answers2

0

You were close to the solution, you can create a Set to contain names of your array1 elements and pushing array1 elements to your result array; subsequently add elements of array2 which names are not contained in your set to your result array :

function removeDuplicates(array1, array2) {
    const result = [];
    const set = new Set();

    for (const elem of array1) {

        result.push(elem);
        set.add(elem.name);
    } 

    for (const elem of array2) {

        if (!set.has(elem.name)) {

            result.push(elem);
        }
    }

    return result;
    
}

const array1 =[
    { phone: "07485454", name: "John" },
    { phone: "054554", name: "Ryan" }
];

const array2 =[
    { phone: "2144564", name: "John" },
    { phone: "286456", name: "Mike" }
];

console.log(JSON.stringify(removeDuplicates(array1, array2)));
dariosicily
  • 4,239
  • 2
  • 11
  • 17
0

you have done a typo mistake in inner for loop condition a < array2.length

Changing the array1 length will results to infinite loop

Every time, when the if(name !== newname) the condition is true, inserting a new object in array1 will results to changing the array1 length.

let array1 =[
    { phone: "07485454", name: "John" },
    { phone: "054554", name: "Ryan" },
]

let array2 =[
    { phone: "2144564", name: "John" },
    { phone: "286456", name: "Mike" },
]

let result = array2.reduce((accumulator, currentItem) =>{
     let index = accumulator.findIndex(item => item.name === currentItem.name);
     if(index === -1){
        accumulator.push(currentItem); 
     } 
    return accumulator;
    },array1);

console.log(result);
Ram
  • 117
  • 1
  • 1
  • 10