javascript - 使用下划线从 JSON obj 收集数据以创建位置数组

标签 javascript jquery json underscore.js

我想知道是否有人可以帮忙解决这个问题..

我正在使用下划线和大量 JSON 数据,并希望提取所有不同的事件位置..

是否可以使用此结构创建所有事件位置的数组? (请参阅 obj 的图像)

我不知道如何循环将每天的 event_location 和 event_location_id 插入数组(如果还没有)——事件...

所以基本上我最终需要的是一个像..这样的列表

[event_location_id: 1, event_location: somewhere]

enter image description here

预先感谢您对此提供的任何帮助。

记下当前数组结果 [Alberto Montellano] enter image description here

最佳答案

您可以使用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/

相关文章:

python - 在 Django : How to serialize dict object to json?

JQuery 在 Controller 上调用 Action(但之后不重定向)

javascript - 在 Node.JS 中解析 Microsoft Office 文件

javascript - 如何使失败的路由解析 promise 加载不同的模板 URL?

javascript - 如何检查 jqm 日历日期在 jQuery Mobile 中是否有事件?

javascript - 如何从 javascript 函数返回 true 和值?

jQuery load() php 文件内的某个 div 或 id?

javascript - 在 json 对象中执行 javascript 代码?

Python 元组到 JSON 输出

Javascript window.print() 导致浏览器挂起