如何从dojo存储的项数组中删除对象

时间:2017-04-20 08:22:00

标签: javascript arrays json dojo

我有一个有序的商店,我正在使用过滤选择,我需要从商店中删除一个商品,如何知道这一点 数组中的对象只有两个属性VALUE和LABEL,

商店就像:

{
    "items":
        [
            {
                "VALUE":"AG",
                "LABEL":"Agriculture"
            },
            {
                "VALUE":"CE",
                "LABEL":"Construction"
            },
            {
                "VALUE":"CO",
                "LABEL":"Consumer"
            }
        ],
    "identifier":"VALUE"
}

我想删除项目数组中的一个对象。

1 个答案:

答案 0 :(得分:2)

有几种方法可以通过示例从数组中删除obj及其安静简单:

  1. Native JS

  2. 使用Array.splice:

    
    
    var json = {"items":[{"VALUE":"AG","LABEL":"Agriculture"},{"VALUE":"CE","LABEL":"Construction"},{"VALUE":"CO","LABEL":"Consumer"},
    {"VALUE":"CD","LABEL":"CDsample"},
    {"VALUE":"AL","LABEL":"Aluminium"}],"identifier":"VALUE"}
    
    var array = json.items;
    var valueToRemove = "CD";
    
    json.items.forEach(function(element,index){
      if(element["VALUE"] == valueToRemove)
        json.items.splice(index, 1);
    });
    
    console.log(json);
    
    
    

    使用Array.filter

    
    
    var json = {"items":[{"VALUE":"AG","LABEL":"Agriculture"},{"VALUE":"CE","LABEL":"Construction"},{"VALUE":"CO","LABEL":"Consumer"},
        {"VALUE":"CD","LABEL":"CDsample"},
        {"VALUE":"AL","LABEL":"Aluminium"}],"identifier":"VALUE"}
    //get the array from
    var array = json.items;
    var valueToRemove = "CD";
    json.items = json.items.filter(function(element) {
        return element["VALUE"] !== valueToRemove;
    });
    
    console.log(json);
    
    
    

    1. 使用Dojo

    2. 使用Array.map:

      
      
      require(["dojo/_base/array"],function(Array){
        var json = {"items":[{"VALUE":"AG","LABEL":"Agriculture"},{"VALUE":"CE","LABEL":"Construction"},{"VALUE":"CO","LABEL":"Consumer"},
        {"VALUE":"CD","LABEL":"CDsample"}, {"VALUE":"AL","LABEL":"Aluminium"}],"identifier":"VALUE"};
      
      
        var valueToRemove = "CD";
        //get index of element
        var index = Array.map(json.items, function(item){
          return item["VALUE"];
        }).indexOf(valueToRemove);
        //remove elemnt at that index
        json.items.splice(index,1);
      
        console.log(json);
      })
      
      <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
      &#13;
      &#13;
      &#13;

      使用Array.filter:

      &#13;
      &#13;
      require(["dojo/_base/array"],function(Array){
        var json = {"items":[{"VALUE":"AG","LABEL":"Agriculture"},{"VALUE":"CE","LABEL":"Construction"},{"VALUE":"CO","LABEL":"Consumer"},
        {"VALUE":"CD","LABEL":"CDsample"}, {"VALUE":"AL","LABEL":"Aluminium"}],"identifier":"VALUE"};
      
      
        var valueToRemove = "CD";
       
       //filter
        json.items = Array.filter(json.items, function(item){
          return item["VALUE"] != valueToRemove;
        });
      
        console.log(json);
      })
      &#13;
      <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
      &#13;
      &#13;
      &#13;

      使用Array.forEach:

      &#13;
      &#13;
      require(["dojo/_base/array"],function(Array){
        var json = {"items":[{"VALUE":"AG","LABEL":"Agriculture"},{"VALUE":"CE","LABEL":"Construction"},{"VALUE":"CO","LABEL":"Consumer"},
        {"VALUE":"CD","LABEL":"CDsample"}, {"VALUE":"AL","LABEL":"Aluminium"}],"identifier":"VALUE"};
      
      
        var valueToRemove = "CD";
      
      
        Array.forEach(json.items, function(item,index){
          if(item)
            if(item["VALUE"] == valueToRemove)
              json.items.splice(index,1);
      
        });
      
        console.log(json);
      })
      &#13;
      <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
      &#13;
      &#13;
      &#13;