I have 2 arrays,
let array1 = ["1", "2", "3"]
let array2 = [{"name": "a", "id" : "1"}, {"name": "b", "id" : "2"}, {"name": "c", "id" : "3"}, {"name": "c", "id" : "4"}]
I want to replace the contents of array1 with elements of array2 when array1 element and array2 id
element's value matches.
Result should be something like this:
array1 = [{"name": "a", "id" : "1"}, {"name": "b", "id" : "2"}, {"name": "c", "id" : "3"}]
I know I can get the result in a separate array, but I want to replace the contents of array1 without having to create a new array.
I tried this:
array1.splice(0, Infinity, ...array2)
based on an answer given to a similar question here
This results in:
array1 = [{"name": "a", "id" : "1"}, {"name": "b", "id" : "2"}, {"name": "c", "id" : "3"}, {"name": "c", "id" : "4"}]
I tried different ways to add a condition to this statement but failed. Is there a way to add a condition to the splice method above? Or if there is a better way of achieving it, do suggest.