0

I try to remove duplicates from this array

let arr = [{id: "2023010600157"},{id: "2023010600158"},{id: "2023010600157"},{id: "2023010600158"}]

After I used reduce(), I found that after the first loop, the change of the prev was unexpected enter image description here Why does prev become the whole array after the first loop?

code here

let arr = [{ id: "2023010600157" },{ id: "2023010600158" },{ id: "2023010600157" },{ id: "2023010600158" } ]

let res = arr.reduce((prev, curr, index) => {

console.warn('loop No ${index + 1}')

console.log(prev);

console.log(curr, 'curr');

if (curr.id !== prev.id) {

prev.push(curr)

}

console.log(prev, 'prev');

return prev

},[])

console.log(res);

i tried to read log ,but i failed.

0 Answers0