Find / delete / add / update objects in nested jso

2020-07-13 08:46发布

问题:

I have a problem with finding object in nested json! I need to do operations like 'add' to object and 'delete' object in that nested json. Would it be easy to get object by using "JSON.stringify" and in that string find objects ID parameter (every object has its own unique ID). Then from that point find its "wrapper" curly braces ({}) i could get object it self and then delete it or add new object in it.

I had this idea, but have no idea how to select its curly braces... I think it might work, but what do you thing? :)

Here would be the example object! https://jsfiddle.net/gb8hb8g7/

var aa = [
    {name: "aaa",
     id: 1,
     items: [
         {name: "bbb",
          id: 15,
          items: [
              {name: "ccc",
               id: 44},
              {name: "ddd",
               id: 91}
          ]},
         {name: "eee",
          id: 12}
     ]
    }
];

console.log(JSON.stringify(aa));

回答1:

You can traverse the nested JSON recursively, to perform the operations you need.

var aa = [
    {name: "aaa",
     id: 1,
     items: [
         {name: "bbb",
          id: 15,
          items: [
              {name: "ccc",
               id: 44},
              {name: "ddd",
               id: 91}
          ]},
         {name: "eee",
          id: 12}
     ]
    }
];


var fff = {name: "fff", id: 13};
addObj(aa, 91, fff);                       // Add obj to same array as item 91
chgObj(aa, 91, '^', 'name', 'zzz');        // Change 'name' property of item 91
chgObj(aa, 91, '+', 'other', 'test');      // Add property to item 91 
chgObj(aa, 91, '+', 'gone', 'delete me');  // Add property to item 91
chgObj(aa, 91, '-', 'gone');               // Delete property from item 91
dltObj(aa, 44);                            // Delete item 44

function addObj(itemArr, nId, newObj) {
    for (var i = 0; i < itemArr.length; i++) {
        if (itemArr[i].id && itemArr[i].id === nId) {
            itemArr.push(newObj);
        } else {
            if (itemArr[i].items) {
                addObj(itemArr[i].items, nId, newObj);
            }
        }
    }
}

function chgObj(itemArr, nId, operator, prop, val) {
    for (var i = 0; i < itemArr.length; i++) {
        if (itemArr[i].id && itemArr[i].id === nId) {
            switch (operator) {
                case '+':
                    if (!itemArr[i][prop]) {
                        itemArr[i][prop] = val;
                    }
                    break;

                case '-':
                    if (itemArr[i][prop]) {
                        delete itemArr[i][prop];
                    }
                    break;

                case '^':
                    if (itemArr[i][prop]) {
                        itemArr[i][prop] = val;
                    }
                    break;
            }
        } else {
            if (itemArr[i].items) {
                chgObj(itemArr[i].items, nId, operator, prop, val);
            }
        }
    }
}

function dltObj(itemArr, nId) {
    for (var i = 0; i < itemArr.length; i++) {
        if (itemArr[i].id && itemArr[i].id === nId) {
            itemArr.splice(i, 1);
        } else {
            if (itemArr[i].items) {
                dltObj(itemArr[i].items, nId);
            }
        }
    }
}

alert(JSON.stringify(aa));

new fiddle: https://jsfiddle.net/ta4pjqew/2



回答2:

You should be able to just use your objects like you are traversing a big array:

    var aa = [
    {name: "aaa",
     id: 1,
     items: [
         {name: "bbb",
          id: 15,
          items: [
              {name: "ccc",
               id: 44},
              {name: "ddd",
               id: 91}
          ]},
         {name: "eee",
          id: 12}
     ]
    }
];

aa[0].name = 'abc';
aa[0].newprop = 23;
console.log(aa[0].items[0].items[1]);
delete  aa[0].items[0].items[1];
console.log(aa[0].items[0].items[1]);


console.log(JSON.stringify(aa));