从JSON数组获取特定字段的数组

时间:2017-05-16 08:05:15

标签: javascript arrays json

所以我得到了一个像这样的JSON数组:

"robots": [
    {
        "id": 1,
        "name": "foo",
        "last_connection": "2017-03-18 14:25:36"
    },{
        "id": 3,
        "name": "bar",
        "last_connection": "2017-03-19 17:28:39"
    },{
        "id": 8,
        "name": "foobar",
        "last_connection": "2017-04-18 11:22:33"
    }
]

是否有简单的方法从我的所有机器人中获取一组id而不在我的阵列上执行for循环?

ids = [1, 3, 8]

1 个答案:

答案 0 :(得分:0)

是的,使用 <span> <div class="col-5"> </div> <div class="col-7"></div> </span>方法。

Array.map

const ids = robots.map(robot => robot.id); 现在为ids