How to loop a simple object?
This question already has an answer here:
It looks like you are trying to get the length of an object, which you can't do. The following should give you what you want.
var links = {
"0": {
"fromOperator": "operator2",
"fromConnector": "output_0",
"fromSubConnector": 0,
"toOperator": "operator1",
"toConnector": "input_0",
"toSubConnector": 0
},
"1": {
"fromOperator": "operator2",
"fromConnector": "output_0",
"fromSubConnector": 0,
"toOperator": "operator0",
"toConnector": "input_0",
"toSubConnector": 0
}
}
for(var i = 0; i < Object.keys(links).length; i++) {
var fromConnector = links[i].fromConnector;
var toConnector = links[i].toConnector;
console.log("FROM: " + fromConnector + " ..... TO: " + toConnector);
}
Note, I updated the limit in the for loop to Object.keys(links).length
.
用一个带有键的对象,从frm零开始并继续键,你可以创建一个数组ald迭代它。
var links = { 0: { fromOperator: "operator2", fromConnector: "output_0", fromSubConnector: 0, toOperator: "operator1", toConnector: "input_0", toSubConnector: 0 }, 1: { fromOperator: "operator2", fromConnector: "output_0", fromSubConnector: 0, toOperator: "operator0", toConnector: "input_0", toSubConnector: 0 } };
Object
.assign([], links)
.forEach(({ fromConnector , toConnector }) =>
console.log("FROM: " + fromConnector + " ..... TO: " + toConnector));
链接地址: http://www.djcxy.com/p/52016.html
上一篇: 使用JavaScript解析Json数组
下一篇: 如何循环一个简单的对象?