If the "id":"236" exists in spConfig['attributs'][125]['options'] get the array that is contained in.
How would you do this in jQuery ?
var spConfig = {
"attributes": {
"125": {
"id": "125",
"code": "pos_colours",
"label": "Colour",
"options": [{
"id": "236",
"label": "Dazzling Blue",
"price": "0",
"oldPrice": "0",
"products": ["11148"]
}, {
"id": "305",
"label": "Vintage Brown",
"price": "0",
"oldPrice": "0",
"products": ["11786", "11787", "11788", "11789", "11790", "11791", "11792", "11793"]
}]
}
}
Your data structure is slightly complicated, but assuming
options.ids are unique, you can useFiddle
The function to return an empty array when a nonexistent key is passed.
Also, if you have more than one
attributeproperty (other than125) and want to iterate over them:Fiddle
Or, if you’ll always access the property
attribute[125], you may as well leave it hard-coded for simplicity:Fiddle
Or passing the
attributeproperty name if you need more customization.Fiddle