I’m a bit new to javascript world and I’m really confused by this piece of simple code and don’t know what is the exact problem.
I want to pull out all the colors and add them to a new array.
here is my code:
const data = [
{a: 'happy', b: 'robin', c: ['blue','green']},
{a: 'tired', b: 'panther', c: ['green','black','orange','blue']},
{a: 'sad', b: 'goldfish', c: ['green','red']}
];
const colors = data.reduce((total,item)=>{
let tempArr = item.c
total.push(...tempArr)
},[])
console.log(colors)
>Solution :
A function that doesn’t contain a return will return undefined. Your reduce callback doesn’t contain a return. Fix with
return total;