jQuery追加到嵌套数组

时间:2018-10-17 13:57:42

标签: javascript jquery arrays json

是否可以使用push或类似方法将JSON对象中的特定嵌套数组作为目标?

以下是JSON对象的示例:

var json = [
        { "Id": 0, "Fruits": [{ "FruitId": 0, "Amount": null }] },
        { "Id": 1, "Fruits": [{ "FruitId": 2, "Amount": { "IsNull": false, "Value": 50.0000 } }, { "FruitId": 8, "Amount": { "IsNull":
false, "Value": 700.0000 } }]}, 
        { "Id": 2, "Fruits": [{ "FruitId": 0, "Amount": null }] },
        { "Id": 3, "Fruits": [{ "FruitId": 0, "Amount": null }] }
    ];

var output = JSON.stringify(json);

var fruitsArray = $.parseJSON(output);

我想将{"FruitId":8,"Amount":50}插入ID为2的Fruits中

在示例中,我看到re push只是将一个对象推到JSON对象的末尾,就像我希望能够动态附加的那样。

任何帮助将不胜感激:-)

3 个答案:

答案 0 :(得分:0)

您首先必须找到索引,然后在该索引上插入数据。

   var json = [
            { "Id": 0, "Fruits": [{ "FruitId": 0, "Amount": null }] },
            { "Id": 1, "Fruits": [{ "FruitId": 2, "Amount": { "IsNull": false, "Value": 50.0000 } }, { "FruitId": 8, "Amount": { "IsNull": false, "Value": 700.0000 } }]}, 
            { "Id": 2, "Fruits": [{ "FruitId": 0, "Amount": null }] },
            { "Id": 3, "Fruits": [{ "FruitId": 0, "Amount": null }] }
        ];


    var indexNo = json.findIndex(function(obj) {
        return obj['Id'] == 2;
    });
    indexNo != -1 ? json[indexNo]['Fruits'].push({"FruitId":8,"Amount":5000}) : "Not found";

--------------使用for循环查找索引-----------

var json = [
            { "Id": 0, "Fruits": [{ "FruitId": 0, "Amount": null }] },
            { "Id": 1, "Fruits": [{ "FruitId": 2, "Amount": { "IsNull": false, "Value": 50.0000 } }, { "FruitId": 8, "Amount": { "IsNull": false, "Value": 700.0000 } }]}, 
            { "Id": 2, "Fruits": [{ "FruitId": 0, "Amount": null }] },
            { "Id": 3, "Fruits": [{ "FruitId": 0, "Amount": null }] }
        ];

var indexNo = -1;
   for(var i = 0; i < json.length; i++) {
            if(json[i]['Id'] == 2) {
                indexNo = i;
                break;
            }
    }
    indexNo != -1 ? json[indexNo]['Fruits'].push({"FruitId":8,"Amount":5000}) : "Not found";

答案 1 :(得分:-1)

另一种方法:

 $.each(json,function(ix,record) {
           if(record.Id == 2)
              {
                  record.Fruits.push({"FruitId":8,"Amount":50})
               }
               });

答案 2 :(得分:-1)

您可以使用函数find查找特定对象,然后将其添加到数组中。

let array = [{    "Id": 0,    "Fruits": [{      "FruitId": 0,      "Amount": null    }]  },  {    "Id": 1,    "Fruits": [{      "FruitId": 2,      "Amount": {        "IsNull": false,        "Value": 50.0000      }    }, {      "FruitId": 8,      "Amount": {        "IsNull": false,        "Value": 700.0000      }    }]  },  {    "Id": 2,    "Fruits": [{      "FruitId": 0,      "Amount": null    }]  },  {    "Id": 3,    "Fruits": [{      "FruitId": 0,      "Amount": null    }]  }],
    targetId = 2,
    object = {"FruitId":8,"Amount":50},
    target = array.find(({Id}) => Id === targetId);

if (target) target.Fruits = [...target.Fruits, object];

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }