javascript - 动态节点生成功能

标签 javascript angularjs underscore.js

var v = [
    {
        "message": {
            "code": "S200",
            "message": "Success"
        },
        "data": {
            "car": [
                {
                    "id": 1,
                    "val": "Toyota"
                },
                {
                    "id": 2,
                    "val": "Honda"
                },
                {
                    "id": 3,
                    "val": "Nissan"
                }
            ],
            "truck": [
                {
                    "id": 1,
                    "val": "Benz"
                },
                {
                    "id": 1,
                    "val": "Volvo"
                }
            ]
        }
    }
]

我有一个如上所示的动态 json。
如何在 undescorejs 或 angularjs 中动态获取 v[0].data.car 节点(不使用 v[0]、v[1] 等)。 Hashmap可以吗?

最佳答案

您需要在 angularjs 中迭代数组,如下所示

v.forEach(function(element){
  console.log(element.data.car[0]);//Over here you can again iterate the car array, I have used the first element instead
});

关于javascript - 动态节点生成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25932393/

相关文章:

javascript - event.target 和 getelementbyid 之间的区别

Javascript/AngularJS 强制数组数据类型

javascript - Select 并不总是与 AngularJS 中的初始模型值匹配

javascript - 测试和编译 Underscore 模板的在线 Playground ?

Javascript - 使函数异步

Javascript定义常量对象属性

angularjs - 切换到 webpack 时出现 Angular "Module is not available"

javascript - 下划线键值对中的求和值

javascript - 使用 underscore.js 在每个数组中的 3 个对象组中对数组值进行分组

javascript - 如何映射任意 Iterables?