How I can access to the models array
when I have only the information "Bridgestone" or "Continental". I think that should works with Object.keys()
and find()
but all my tries didn't worked. I think the trick is to get the key and with this key you can iterate with forEach()
the models.
json_structure = {
"tyres":[
{
"manufacture":"Bridgestone",
"models":[
"Potenza",
"Turanza"
]
},
{
"manufacture":"Continental",
"models":[
"Allseasonconta",
"Winter Contact"
]
}
]
}
Just use find
method to find a manufacture by its name, and then get the models array out of it:
const jsonStructure = {
"tyres":[
{
"manufacture":"Bridgestone",
"models":[
"Potenza",
"Turanza"
]
},
{
"manufacture":"Continental",
"models":[
"Allseasonconta",
"Winter Contact"
]
}
]
}
const getModelsByManufactureName = data => name => {
const manufacture = data.tyres.find(val => val.manufacture === name)
if (!manufacture) return manufacture
return manufacture.models
}
console.log(getModelsByManufactureName(jsonStructure)('Bridgestone'))