ANGULARJS在数组元素中搜索并从数组中删除相应的数据

时间:2018-03-26 07:15:12

标签: arrays angularjs json

我正在使用angularjs。我的json是

[
  {
    "Workflow_version_id": 1,
    "data": {
      "Workflow_version_id": 1,
      "workflow_id": 1,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 2,
    "data": {
      "Workflow_version_id": 2,
      "workflow_id": 2,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 3,
    "data": {
      "Workflow_version_id": 3,
      "workflow_id": 3,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 4,
    "data": {
      "Workflow_version_id": 4,
      "workflow_id": 5,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  }
]

我只想搜索整个json包含或拥有节点Workflow_version_idvalue 2。如果显示该值,我想删除相同.eg的数据 如果我当前Workflow_version_id is 2我想删除id 2的完整详细信息

{
    "Workflow_version_id": 2,
    "data": {
      "Workflow_version_id": 2,
      "workflow_id": 2,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  }

然后我的最后一个json就像

[
  {
    "Workflow_version_id": 1,
    "data": {
      "Workflow_version_id": 1,
      "workflow_id": 1,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 3,
    "data": {
      "Workflow_version_id": 3,
      "workflow_id": 3,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 4,
    "data": {
      "Workflow_version_id": 4,
      "workflow_id": 5,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  }
]

5 个答案:

答案 0 :(得分:1)

这是更新后的答案。查看工作代码:http://jsfiddle.net/nwa00chg/

Array.prototype.removeAt = function(id) {
    for (var item in this) {
        if (this[item].Workflow_version_id == id) {
            this.splice(item, 1);
            return true;
        }
    }
    return false;
}

答案 1 :(得分:0)

使用filter和hasOwnProperty检查密钥是否存在是您需要的。



var str = [
  {
    "Workflow_version_id": 1,
    "data": {
      "Workflow_version_id": 1,
      "workflow_id": 1,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 2,
    "data": {
      "Workflow_version_id": 2,
      "workflow_id": 2,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 3,
    "data": {
      "Workflow_version_id": 3,
      "workflow_id": 3,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  },
  {
    "Workflow_version_id": 4,
    "data": {
      "Workflow_version_id": 4,
      "workflow_id": 5,
      "version_number": 1,
      "subversion_number": 1,
      "Workflow_definition": 1,
      "published": true,
      "del_flag": true,
      "locked_on": "asdasd",
      "locked_by": "asasas",
      "version_comments": "ASasaaaa"
    }
  }
];

var updatedJson = str.filter(function(data) {
    return data.hasOwnProperty("Workflow_version_id");

})
console.log(updatedJson);




答案 2 :(得分:0)

您可以使用简单的JavaScript来完成。

  

假设你的对象的var data = [] //数组。

var index = data.findIndex(function(o){ return o.Workflow_version_id == 2});

变量索引将具有object的索引。

  

从索引

中删除对象
data.splice(index,1) //after that data object will not have that object.

如果数组没有唯一ID,则可以使用array#filter,它将删除满足条件的所有元素。

答案 3 :(得分:0)

请检查一下。希望这会有所帮助

$scope.Data.myData=[
{
"Workflow_version_id": 1,
"data": {
  "Workflow_version_id": 1,
  "workflow_id": 1,
  "version_number": 1,
  "subversion_number": 1,
  "Workflow_definition": 1,
  "published": true,
  "del_flag": true,
  "locked_on": "asdasd",
  "locked_by": "asasas",
  "version_comments": "ASasaaaa"
}
},
{
"Workflow_version_id": 2,
"data": {
  "Workflow_version_id": 2,
  "workflow_id": 2,
  "version_number": 1,
  "subversion_number": 1,
  "Workflow_definition": 1,
  "published": true,
  "del_flag": true,
  "locked_on": "asdasd",
  "locked_by": "asasas",
  "version_comments": "ASasaaaa"
}
},
{
"Workflow_version_id": 3,
"data": {
  "Workflow_version_id": 3,
  "workflow_id": 3,
  "version_number": 1,
  "subversion_number": 1,
  "Workflow_definition": 1,
  "published": true,
  "del_flag": true,
  "locked_on": "asdasd",
  "locked_by": "asasas",
  "version_comments": "ASasaaaa"
}
},
{
"Workflow_version_id": 4,
"data": {
  "Workflow_version_id": 4,
  "workflow_id": 5,
  "version_number": 1,
  "subversion_number": 1,
  "Workflow_definition": 1,
  "published": true,
  "del_flag": true,
  "locked_on": "asdasd",
  "locked_by": "asasas",
  "version_comments": "ASasaaaa"
}
}
]
 $scope.filterData = function (SourceArray, SourceColumnname, searchValue) {
    var tempArray = [];
    for (var i = 0; i < SourceArray.length; i++) {
        if (SourceArray[i][SourceColumnname] == searchValue) {
            tempArray.push(SourceArray[i]);
        }
    }
    return tempArray;
};
var filteredData= $scope.filterData($scope.Data.myData, "Workflow_version_id", 3);

答案 4 :(得分:-1)

您可以使用array#filter并将Workflow_version_id与您要删除的值进行比较。

&#13;
&#13;
var data = [ { "Workflow_version_id": 1, "data": { "Workflow_version_id": 1, "workflow_id": 1, "version_number": 1, "subversion_number": 1, "Workflow_definition": 1, "published": true, "del_flag": true, "locked_on": "asdasd", "locked_by": "asasas", "version_comments":"ASasaaaa" } }, { "Workflow_version_id": 2, "data": { "Workflow_version_id": 2, "workflow_id": 2, "version_number": 1, "subversion_number": 1, "Workflow_definition": 1, "published": true, "del_flag": true, "locked_on": "asdasd", "locked_by": "asasas","version_comments": "ASasaaaa" } }, { "Workflow_version_id": 3, "data": { "Workflow_version_id": 3, "workflow_id": 3, "version_number": 1, "subversion_number": 1, "Workflow_definition": 1, "published": true, "del_flag": true, "locked_on": "asdasd", "locked_by":"asasas", "version_comments": "ASasaaaa" } }, { "Workflow_version_id": 4, "data": { "Workflow_version_id": 4, "workflow_id": 5, "version_number": 1, "subversion_number": 1, "Workflow_definition": 1, "published": true, "del_flag": true, "locked_on": "asdasd",
"locked_by": "asasas", "version_comments": "ASasaaaa" } } ];
data = data.filter(o => o.Workflow_version_id !== 2);
console.log(data);
&#13;
&#13;
&#13;