I have an array of objects that looks like that:
const myArr = [
{id: 1,
some_data: {
"color_1": {
"value": "red"
},
"color_2": {
"value": "blue"
},
"color_3": {
"value": "yellow"
},
"shape_1": {
"value": "square"
},
"shape_2": {
"value": "circle"
}
}
},
{id: 2,
some_data: {
"color_1": {
"value": "pink"
},
"color_2": {
"value": "orange"
},
"color_3": {
"value": "yellow"
},
"shape_1": {
"value": "square"
},
"shape_2": {
"value": "triangle"
}
}
},
{id: 3,
/* some_data: {...}*/},
{id: 4,
/* some_data: {...}*/}
]
I want to go over myArr and extract the values that correspond to colors, to end up with this array:
// desired output
["red", "blue", "yellow", "pink", "orange", "yellow"]
So I figured out that I would need to use String.prototype.startsWith() on the keys. Unfortunately, I have been unsuccessful in all my attempts to map over myArr.
Inspired by this answer I tried:
const oneAttempt = myArr.map((obj) =>
Object.keys(obj.some_data)
.filter((v) => v.startsWith('color'))
.map((e) => obj.some_data[e].value)
);
But it's not quite what I need. It's probably a pretty simple thing but I'm failing to figure this out.