-2

How to find and remove an elem that match in two different arrays?

For e.g.

let arr = [
    { startTimestamp: 1602675569525, name: "Pip" },
    { startTimestamp: 1602664708822, name: "Jimmy" },
    { startTimestamp: 1234, name: "Hero" }
];

let arr2 = [
    { startTimestamp: 1602675569525, name: "Pip" },
    { startTimestamp: 1602664708822, name: "Jimmy" }, 
    { startTimestamp: 505050, name: "Kit" }

];

If the startTimestamp match then I would like to remove those elem. Output should be

[
   { startTimestamp: 1234, name: "Hero" },
   { startTimestamp: 505050, name: "Kit" }
]

I've tried this below but it only gives me [{startTimestamp: 1234, name: "Hero"}] as I am only pushing arr[i] but I also need { startTimestamp: 505050, name: "Kit" }

let newArr = []; 

for (let i = 0; i < arr.length; i++) {
    if(arr[i].startTimestamp !== arr2[i].startTimestamp) {
            newArr.push(arr[i])
     }
    
}

Jereme
  • 375
  • 2
  • 9
  • What have you tried so far to solve this on your own? What problems do you have with that approach? And why do you ask us for an (opinion-based) "best approach"? – Andreas Oct 14 '20 at 13:32
  • @Andreas thanks for your comment. really only need a way rather than best approach. – Jereme Oct 14 '20 at 13:50
  • Does this answer your question? [Finding and removing matching and corresponding values in an array](https://stackoverflow.com/questions/60082255/finding-and-removing-matching-and-corresponding-values-in-an-array) – TylerH Oct 15 '20 at 14:50
  • Also dupe of https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array and https://stackoverflow.com/questions/53534721/find-and-remove-first-matching-element-in-an-array-of-javascript-objects – TylerH Oct 15 '20 at 14:50

2 Answers2

2

Best would be to merge them, and then filter on doubles:

const newArr = [...arr, ...arr2].filter(
  (item, index, self) => item.findIndex(
    (prev) => prev.startTimestamp === item.startTimestamp
  ) === index
);
Poul Kruijt
  • 58,329
  • 11
  • 115
  • 120
1

It will be good to merge two arrays and filter the items by removing the duplicated startTimestamp objects.

let arr = [
    { startTimestamp: 1602675569525, name: "Pip" },
    { startTimestamp: 1602664708822, name: "Jimmy" },
    { startTimestamp: 1234, name: "Hero" }
];

let arr2 = [
    { startTimestamp: 1602675569525, name: "Pip" },
    { startTimestamp: 1602664708822, name: "Jimmy" }, 
    { startTimestamp: 505050, name: "Kit" }
];

const result = [ ...arr, ...arr2 ].filter((item, index, origin) => origin.filter((subItem) => subItem.startTimestamp === item.startTimestamp).length === 1);
console.log(result);
Derek Wang
  • 9,675
  • 4
  • 14
  • 36
  • thanks for your comment that worked. If you have time could you please help me with another use case? If there are duplicates I want to keep only one of it. so referencing to the same array data above. output would be ``` [ { startTimestamp: 1602675569525, name: "Pip" }, { startTimestamp: 1602664708822, name: "Jimmy" }, { startTimestamp: 1234, name: "Hero" }, { startTimestamp: 505050, name: "Kit" } ] ``` Thanks – Jereme Oct 14 '20 at 14:33
  • There is no duplicates on each array? So arr1 has no duplicated `startTimestamp` itself and same as `arr2`? – Derek Wang Oct 14 '20 at 14:37
  • 1
    Anyway, you can do like this. `const val = [ ...arr, ...arr2 ].reduce((acc, cur) => { if (!acc[cur.startTimestamp]) { acc[cur.startTimestamp] = cur.name; } return acc; }, {}); console.log(Object.entries(val).map(([ startTimestamp, value ]) => ({ startTimestamp, ...value })));` – Derek Wang Oct 14 '20 at 14:42
  • say after we merge arr1 and arr2. At this point we'll have duplicates this happens in my app. Then, I want to just keep one of the duplicates and also keep the non duplicate value. – Jereme Oct 14 '20 at 14:44
  • Above will answer your second question. – Derek Wang Oct 14 '20 at 14:52