I would like to add all the pink values of the objects below to the pink array as items.
const mainObj = [
{
image: 'test_icon.jpg',
colour: "pink",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "pink",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "pink",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "pink",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "green",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "green",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "blue",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "blue",
text: "text",
link: ""
},
{
image: 'test_icon.jpg',
colour: "green",
text: "text",
link: ""
}
];
What i have so far:
for(let i = 0; i < mainObj.length; i++) {
if(mainObj[i].colour == "pink") {
const pink = [];
pink.push(mainObj[i].colour);
}
}
The problem I have is that at the moment it's not pushing all the pink values in one pink array but it individually creates a pink array and adds one to each.
.colour? That will just be "pink" and seems pretty useless…"pink"which is a guessable answer and is not a evaluated answer based on input array. the only thing is useful in the output is the length of your array denoting the number of objects having pink color.