-1

consider below 2 array of objects, say arr1 and arr2. based on id, if any new item exist in arr2 copy item to arr1 and remove item from arr1 if that item not present in arr2. Tried something like this,

const arr1 = [
  {name: "name1", id: 1},
  {name: "name2", id: 2},
  {name: "name3", id: 3}
];
    
const arr2 = [
  {name: "name1", id: 1},
  {name: "name2", id: 4},
  {name: "name3", id: 3}
];

const array1 = arr2.filter((val) => {
  return arr1.find((val2) => {
    const arr = [];
    if (!arr1.includes(val.id)) {
      arr.push(val);
    }
  });
});

console.log(array1)

required output

output = `[ 
{name: "name1", id: 1},
{name: "name3", id: 3},
{name: "name2", id: 4} 
]`
2
  • in other words, what have you tried? Commented Aug 6, 2024 at 10:17
  • array1 = arr2.filter((val) => { return arr1.find((val2) => { const arr = []; if (!arr1.includes(val.id)) { arr.push(val); } }); }); i was trying something like this, but it is not feasible. Commented Aug 6, 2024 at 10:28

2 Answers 2

1

EDIT:

Reduce method, based on "id" may be used but note that ordering does not comply with your expected result as commented below by Hao Wu

const arr1 = [
      {name: "name1", id: 1},
      {name: "name2", id: 2},
      {name: "name3", id: 3}
    ];

    const arr2 = [
      {name: "name1", id: 1},
      {name: "name2", id: 4},
      {name: "name3", id: 3}
    ];

let output = arr2.reduce((a,b) => {
    let a1 = arr1.find(e => e.id === b.id) || {};
    return a.concat(Object.assign(a1, b));
},[]);

console.log(output)

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

4 Comments

OP wanted it to be based on id but you're comparing name, also the order is off.
Thank you for the solution , this solution works for the problem statement i posted, how can i preserve the values of arr1 properties ? @Haluk
What do you mean by "preserving properties", can you give an example?
Sorry for late reply, above method replacing values of name from arr1 with arr2 values, ex: arr1 = [{name:"jhon", id:1}]; and arr2 = [{name:'', id:1}]; above function gives out put as [{name:"", id:1}]; i want to preserve values of arr1. @Haluk
1

You can use below code:

    const arr1 = [
      { name: "name1", id: 1 },
      { name: "name2", id: 2 },
      { name: "name3", id: 3 }
    ];
    
    const arr2 = [
      { name: "name1", id: 1 },
      { name: "name2", id: 4 },
      { name: "name3", id: 3 }
    ];
    
    var idsInArr2 = new Set(arr2.map(item => item.id));
    
    // Remove items from arr1 that are not in arr2
    var updatedArr1 = arr1.filter(item => idsInArr2.has(item.id));
    
    // Create a set of IDs from updatedArr1 for quick lookup
    var idsInUpdatedArr1 = new Set(updatedArr1.map(item => item.id));
    
    // Add items to arr1 that are in arr2 but not in updatedArr1
    arr2.forEach(item => {
      if (!idsInUpdatedArr1.has(item.id)) {
        updatedArr1.push(item);
      }
    });
    
    console.log(updatedArr1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.