I am trying to turn a array that exists of objects with a nested array inside it into a new array that consist of the values inside the nested array separately(Might sound very complicated but example show very clear)
What I have:
const values = [{
geometry: {
"coordinates": [11.4828, 59.1264],
"Type": "point"
},
properties: {
Region: "Oklahoma",
sales: {
donuts: {
2005: 5,
2006: 8,
2007: 10
},
apples: {
2005: 10,
2006: 8,
2007: 10
}
}
}
},
{
geometry: {
"coordinates": [9.4828, 76.1264],
"Type": "point"
},
properties: {
Region: "Texas",
sales: {
donuts: {
2005: 8,
2006: 0,
2007: 7
},
apples: {
2005: 7,
2006: 9,
2007: 4
}
}
}
}
]
const filterValue = "donuts"
What My goal is:
newValues =[{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:5,time:2005}},{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:8,time:2006}},{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:10,time:2007}} AND SO ON FOR ALL THE values that are in const donuts/filter for each value. Could also switch out value for donuts if that is easier
}
]
What I have tried so far:
const newObject = []
values.map((value)=>{
console.log(value.properties.sales.donuts)
for (var key in value.properties.sales.donuts) {
if (value.properties.sales.donuts.hasOwnProperty(key)) {
newObject.push({...value,value.properties:{[key]:value.properties.sales.donuts[key]})
}
}
})
console.log(newObject)