I’m having trouble with my JSON data structure and need help figuring out what’s wrong.
Here’s my sample data:
var inventory = {
"items": [
{
"details": [
{
"id": "001",
"cost": "250",
"code": "ABC123XYZ",
"brand": "Samsung",
"title": "Device A",
"info": "This platform consists of two main sections: the frontend (visible to users) and the backend (used for management tasks). Access to the backend requires separate login credentials.",
"category_id": "1",
"stock": "50"
}
],
"variants": [
{
"color": [
{"green": "+15", "available": "3"},
{"black": "+25", "available": "1"}
]
},
{
"size": [
{"Small": "+5", "available": "2"},
{"Medium": "+15", "available": "6"}
]
}
]
}
],
"sections": [
{"id": 1, "title": "Section A", "info": "First section details"},
{"id": 2, "title": "Section B", "info": "Second section details"}
]
};
This works perfectly:
inventory.sections.each(function(section){
var option = new Option(section.title, section.id);
try {
dropdown.add(option, null)
} catch (error) {
dropdown.add(option)
}
});
But this returns undefined:
inventory.items.each(function(item){
var wrapper = new Element('div.card'),
title = new Element('h2', {
'html': '<a href="#item/' + parseInt(item.details.id) + '">' + item.details.title + '</a>'
}).inject(wrapper),
description = new Element('p', {'html': item.details.info}).inject(title, 'after');
wrapper.inject(container);
});
Why does the second code block fail while the first one works? The structure looks similar to me but I must be missing something about how to access nested arrays properly.