循环浏览动态嵌套javascript对象

时间:2018-12-17 15:24:30

标签: javascript arrays object

我有一个动态的对象数组,如下所示:

[
    0: {id: "1", name: "name1", client: "data"},
    1: {id: "2", name: "name2", client: "data"},
    2: {id: "4", name: "name3", client: "data"},
    3: {id: "5", name: "name4", client: "data"},
    4: {id: "6", name: "name5", client: "data"},
    5: {id: "7", name: "name6", client: "data"}
]

我想从该表中提取值,例如name1,name2 ...和数据...

提前谢谢! :)

1 个答案:

答案 0 :(得分:0)

您可以使用此:

let arr =[
     {id: "1", name: "name1", client: "data"},
     {id: "2", name: "name2", client: "data"},
     {id: "4", name: "name3", client: "data"},
     {id: "5", name: "name4", client: "data"},
     {id: "6", name: "name5", client: "data"},
     {id: "7", name: "name6", client: "data"}
];

for (obj in arr) {
    console.log(arr[obj]["name"]); // will get the name. 
}

您还可以在循环内进行操作:let client = arr[obj]["client"];(例如,如果需要客户端值)