就个人而言,我宁愿使用forEach进行此类操作。我会这样做:
var json = {“tsn”: {
“events”: [
{
“title”: “Lorem ipsum”,
“description”: “Dolor sit”
},
{
“title”: “Duis aute irure”,
“description”: “eu fugiat nulla pariatur”
},
],
“occurrence”: [
“Music”,
“Party”
]
}
};
var events = json.tsn.events;
// loop to iterate through array of tsn events
events.forEach(function(item){
console.log(item.title); // to print each of the titles
item[“image”] = “yourImage.jpg”; // will add to each item the image
// … do any other item specific operation
});
</code>
为了迭代事件,我会在不同的forEach中做同样的事情,因为它们都有不同的长度。