我想知道是否有人可以帮忙解决这个问题..
我正在使用下划线和大量 JSON 数据,并希望提取所有不同的事件位置..
是否可以使用此结构创建所有事件位置的数组? (请参阅 obj 的图像)
我不知道如何循环将每天的 event_location 和 event_location_id 插入数组(如果还没有)——事件...
所以基本上我最终需要的是一个像..这样的列表
[event_location_id: 1, event_location: somewhere]
预先感谢您对此提供的任何帮助。
记下当前数组结果 [Alberto Montellano]
最佳答案
您可以使用underscore库的.each函数。 http://underscorejs.org/#each
我的建议是:
var arrayResult = [];
_.each(yourObject.days, function(day, key) {
_.each(day.events, function(event, key) {
var obj = {
event_location_id : event.event_location_id,
event_location: event.event_location
};
arrayResult.push(obj);
});
});
您的结果列表将是:
[{event_location_id: 1, event_location: somewhere}, {event_location_id: 2, event_location: somewhere2}, {event_location_id: 3, event_location: somewhere3} ]
希望对您有所帮助。
关于javascript - 使用下划线从 JSON obj 收集数据以创建位置数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016831/