如何循环一个简单的对象?

这个问题在这里已经有了答案:

  • 如何通过一个普通的JavaScript对象与作为成员的对象进行循环? 17个答案

  • 它看起来像你试图获得一个对象的长度,你不能这样做。 以下应该给你你想要的。

    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);
    }
    

    请注意,我将for循环中的限制更新为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/52015.html

    上一篇: How to loop a simple object?

    下一篇: Referencing / Parsing an object