I compare id with two array with object.
Here is my function:
array1 = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
array2 = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
const compareFunction = (array1, array2) => {
array2.map((allValue) => {
array1.map((value) => {
allValue.selected = value.id === allValue.id;
});
})
return array2;
}
I think I will get the array2 like
[{ id: 1, selected: true }, { id: 2, selected: true },{ id: 3, selected: true }]
but actually array2 become
[{ id: 1, selected: false }, { id: 2, selected: false },{ id: 3, selected: true }]
Only the last array argument selected become true.
Which step was wrong ? Thanks.
selectedto betrueif theidat one index matches theidat the same index in the other array, or what?array1sometime will be[{ id: 1 }]or[{ id: 1 }, { id: 2 }]or[{ id: 1 }, { id: 2 }, { id: 3 }][{ id: 2 }, { id: 3 }], would you want noselecteds, or two?)for-eachloop value ->{ id: 1 }{ id: 2 }{ id: 3 }