从json arry中删除一些数据

时间:2018-02-28 06:52:59

标签: javascript arrays json

这是我的json数组。这是在我在下拉列表中选择一些元素时创建的。然后可以更改数组元素的数量

"Actions":[  
      {  
         "key":"AC_001",
         "tag":"AWARD_POINTS",
         "name":"Award Points",
         "content":{  
            "html":"<div class='col-sm-4'><label for='point-amount'>Amount</label><input type='number' class='form-control' id='point-amount' placeholder='Add Points'></div>",
            "actions":""
         },
         "identify":"",
         "value":"",
         "uuid":"f0a8ae1f-9c2c-9cc3-eb38-7a7ab0570dd1"
      },
      {  
         "key":"AC_003",
         "tag":"SEND_MAIL",
         "name":"Send Mail",
         "content":{  
            "html":""
         },
         "identify":"",
         "value":"",
         "uuid":"83af6438-a292-a34b-e435-8795281ec393"
      }
   ]

我想在这里删除一些标签,只显示几个标签。但我知道如何做到这一点。有人帮我吗?

2 个答案:

答案 0 :(得分:2)

如果您想要移除数组中的某些元素并且不将它们包含在您的下拉列表中,则可以使用filter

假设您不希望包含以下带有标记

的数组元素
let doNotInclude = ['DELETE_THIS_TAG', 'TAG_TO_DELETE'];

您可以使用filterincludes制作要在下拉列表中使用的新数组。

&#13;
&#13;
let obj = {
  "Actions": [{
      "key": "AC_001",
      "tag": "AWARD_POINTS",
      "name": "Award Points",
      "content": {
        "html": "<div class='col-sm-4'><label for='point-amount'>Amount</label><input type='number' class='form-control' id='point-amount' placeholder='Add Points'></div>",
        "actions": ""
      },
      "identify": "",
      "value": "",
      "uuid": "f0a8ae1f-9c2c-9cc3-eb38-7a7ab0570dd1"
    },
    {
      "key": "AC_003",
      "tag": "DELETE_THIS_TAG",
      "name": "Delete",
      "content": {
        "html": ""
      },
      "identify": "",
      "value": "",
      "uuid": "83af6438-a292-a34b-e435-8795281ec393"
    },
    {
      "key": "AC_003",
      "tag": "SEND_MAIL",
      "name": "Send Mail",
      "content": {
        "html": ""
      },
      "identify": "",
      "value": "",
      "uuid": "83af6438-a292-a34b-e435-8795281ec393"
    },
    {
      "key": "AC_003",
      "tag": "TAG_TO_DELETE",
      "name": "Delete",
      "content": {
        "html": ""
      },
      "identify": "",
      "value": "",
      "uuid": "83af6438-a292-a34b-e435-8795281ec393"
    },
  ]
}

let doNotInclude = ['DELETE_THIS_TAG', 'TAG_TO_DELETE'];

let newObj = obj.Actions.filter(v => !doNotInclude.includes(v.tag));

console.log(newObj);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以通过以下模式删除动态生成的json数组中的任何元素

&#13;
&#13;
var data = {
	"Actions":[  
      {  
         "key":"AC_001",
         "tag":"AWARD_POINTS",
         "name":"Award Points",
         "content":{  
            "html":"<div class='col-sm-4'><label for='point-amount'>Amount</label><input type='number' class='form-control' id='point-amount' placeholder='Add Points'></div>",
            "actions":""
         },
         "identify":"",
         "value":"",
         "uuid":"f0a8ae1f-9c2c-9cc3-eb38-7a7ab0570dd1"
      },
      {  
         "key":"AC_003",
         "tag":"SEND_MAIL",
         "name":"Send Mail",
         "content":{  
            "html":""
         },
         "identify":"",
         "value":"",
         "uuid":"83af6438-a292-a34b-e435-8795281ec393"
      }
   ]
}
 
for(var x=0; x<data.Actions.length; x++) 
{   
	var key = "tag";
	delete data.Actions[x][key];
	console.log(data.Actions[x].tag); //  should be undefined
    console.log(data.Actions[x].name); //  will be return value from array
} 
&#13;
&#13;
&#13;