通过嵌套的JSON数组对象nodejsparsing

我试图parsing一个JSON对象,并删除其中的密钥ID,我能够删除出现在对象的根目录,但无法遍历的嵌套数组对象,也有' id'键并删除它们,现在跟在代码块之后

var json = { "id" : "a28b469b-b4f2-4846-9b5f-9d866f249bbe", "description" : "Cost of Product", "periodicity" : "calendar-monthly", "Vanilla" : [ { "id" : "22382c50-f56f-40b7-a308-203da052c5bc", "price" : { "amount" : 100.000, "currency" : "USD" }, "packing" : "RECURRING", "billedInAdvance" : true } ], "Chocolate" : [ { "id" : "44672921-1966-456e-bde2-87ef72f31cab", "price" : { "amount" : 256.000000, "currency" : "USD" }, "packing" : "Box_Usage" } ], "Peach" : [ { "id" : "e3a600e2-a2ed-4872-8e6d-5d59ec5ca02d", "packing" : "Box_Usage", "diff" : [ { "pricePerUnit" : { "amount" : 25.000000, "currency" : "USD" }, "fixedPrice" : { "amount" : 36.000000, "currency" : "USD" } } ] } ], "Strawberry" : [ { "id" : "43b4a121-455a-4828-b4bf-1bacda49f9ce", "packing" : "Box_Usage", "diff" : [ { "pricePerUnit" : { "amount" : 100.000000, "currency" : "USD" } } ] } ] 

}

我能够通过索引来访问数组对象中的'id'属性,但是当JSON中的键值增长时,这不会处理dynamic场景。 任何build议都是有价值的

你可以recursion地做到这一点:每当你在对象中find一个数组时,你通过循环来删除每个元素中的id。

 var json = { "id" : "a28b469b-b4f2-4846-9b5f-9d866f249bbe", "description" : "Cost of Product", "periodicity" : "calendar-monthly", "Vanilla" : [ { "id" : "22382c50-f56f-40b7-a308-203da052c5bc", "price" : { "amount" : 100.000, "currency" : "USD" }, "packing" : "RECURRING", "billedInAdvance" : true } ], "Chocolate" : [ { "id" : "44672921-1966-456e-bde2-87ef72f31cab", "price" : { "amount" : 256.000000, "currency" : "USD" }, "packing" : "Box_Usage" } ], "Peach" : [ { "id" : "e3a600e2-a2ed-4872-8e6d-5d59ec5ca02d", "packing" : "Box_Usage", "diff" : [ { "pricePerUnit" : { "amount" : 25.000000, "currency" : "USD" }, "fixedPrice" : { "amount" : 36.000000, "currency" : "USD" } } ] } ], "Strawberry" : [ { "id" : "43b4a121-455a-4828-b4bf-1bacda49f9ce", "packing" : "Box_Usage", "diff" : [ { "pricePerUnit" : { "amount" : 100.000000, "currency" : "USD" } } ] } ] }; function removeId(obj) { delete obj.id; Object.keys(obj).forEach(key => { if (Array.isArray(obj[key])) { obj[key].forEach(o => { removeId(o); }); } }); } removeId(json); console.log(json);