猿问

如何通过关键字删除json数组中的所有对象

我有这样的json:


json = [ 

   { 

      "value1":"3863",

      "value2":"4567"

   },

   { 

      "value1":"4456",

      "value2":"87687"

   },

   { 

      "value1":"98494",

      "value2":"4534"

   },   

]


我需要的是删除 value2 以便 json 看起来像:


json = [ 

   { 

      "value1":"3863"

   },

   { 

      "value1":"4456"

   },

   { 

      "value1":"98494"

   },   

]


我试过用


for(var i = 0; i < json.length; i++)

{    

  delete json["value2"];   

}


但它不起作用。


有没有办法做到这一点?


梦里花落0921
浏览 158回答 3
3回答

皈依舞

const json = [&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; "value1":"3863",&nbsp; &nbsp; &nbsp; "value2":"4567"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; "value1":"4456",&nbsp; &nbsp; &nbsp; "value2":"87687"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; "value1":"98494",&nbsp; &nbsp; &nbsp; "value2":"4534"&nbsp; &nbsp;},&nbsp; &nbsp;];json.forEach(item => delete item.value2);

慕森卡

使用您当前的语法:for(var i = 0; i < json.length; i++){&nbsp; &nbsp;&nbsp;&nbsp; delete json[i].value2;&nbsp; &nbsp;}

慕运维8079593

您只是错过i了访问 json 数组元素的用法:for(var i = 0; i < js.length; i++){&nbsp; &nbsp;&nbsp;&nbsp; delete json[i]["value2"];&nbsp; &nbsp;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答