0

I have the following code:

const data = [
  [{
      id: 1,
      done: false
    },
    {
      id: 2,
      done: true
    }
  ],
  [{
      id: 3,
      done: false
    },
    {
      id: 4,
      done: false
    }
  ],
  [{
      id: 5,
      done: true
    },
    {
      id: 6,
      done: false
    }
  ],
  [{
      id: 7,
      done: true
    },
    {
      id: 8,
      done: false
    }
  ]
]

//pV=previous value, cV=current value, cI=current index
const result = data.reduce((pV, cV, cI) => (cV.some(obj => obj.done) && pV.push(cI), pV), []);

console.log(result);

I understand the way how reduce works, but I don't get it how the return value of callback is composed here:

(cV.some(obj => obj.done) && pV.push(cI), pV)

I mean yes it checks if cV has some object where done is true and pushes cI to pV(the accumulator), but why is pV comma separated at the end? How does this expression return a pV array?

charly1212
  • 724
  • 6
  • 16

0 Answers0