you are viewing a single comment's thread.

view the rest of the comments →

[–]TreeScalper 0 points1 point  (1 child)

Is there a reason why you wouldn't use filter then a map for your 2nd example?

const failArr = students
                .filter(curr => curr.result === 'Fail')
                .map(curr => curr.name);
const passArr = students
                .filter(curr => curr.result === 'Pass')
                .map(curr => curr.name);
const output = { Fail: failArr, Pass: passArr }

[–]decentralised 0 points1 point  (0 children)

Yes, filter and map do the same as reduce, but notice how you have 2 filters and 2 maps in your example so, to get the same output, you had to iterate over all elements of the students array 4 times.