如何在javascript中从列表中删除行
我想从列表中删除一行。最初,下面的代码就在那里如何在javascript中从列表中删除行,javascript,angularjs,Javascript,Angularjs,我想从列表中删除一行。最初,下面的代码就在那里 var newArray = _.filter($scope.componentList, function(arrayItem) { return rowId !== arrayItem.rowId; }); $scope.componentList = newArray; 这个过滤器将做的是,如果返回值为true,那么过滤器将包含该对象,否则它将删除该对象。现在我只想为家长和孩子返回相同的值 此处rowId是此函数的输入。 在上面的中,$sc
var newArray = _.filter($scope.componentList, function(arrayItem) {
return rowId !== arrayItem.rowId;
});
$scope.componentList = newArray;
这个过滤器将做的是,如果返回值为true,那么过滤器将包含该对象,否则它将删除该对象。现在我只想为家长和孩子返回相同的值
此处rowId
是此函数的输入。
在上面的中,$scope.componentList=newArray代码>行我们将得到任何rowId
处理过的内容,该对象将被删除。剩下的rowId将出现在该列表中。很好
return rowId !== arrayItem.rowId;
在这一行中,返回true的内容将出现在newArray中
但是现在格式改变了。现在的格式就像下面的咆哮
[
{
"revision": 0,
"componentName": "abc",
"componentIdentification": "abc",
"componentType": "1",
"componentState": "1",
"componentUrl": null,
"componentId": "214",
"rowId": "3",
"items": [
{
"revision": 0,
"componentName": "efg",
"componentIdentification": "efg",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "215",
"rowId": "3.1",
"items": null,
"componentStateId": 0,
"ctastatus": 0,
"actionId": "16",
"actionToPerform": "1"
}
],
"componentStateId": 0,
"ctastatus": 0,
"actionId": "37",
"actionToPerform": "1"
},
{
"revision": 0,
"componentName": "hij",
"componentIdentification": "hij",
"componentType": "1",
"componentState": "1",
"componentUrl": null,
"componentId": "206",
"rowId": "1",
"items": [
{
"revision": 0,
"componentName": "klm",
"componentIdentification": "klm",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "207",
"rowId": "1.1",
"items": [
{
"revision": 0,
"componentName": "nop",
"componentIdentification": "nop",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "208",
"rowId": "1.1.1",
"items": [
{
"revision": 0,
"componentName": "qrs",
"componentIdentification": "qrs",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "209",
"rowId": "1.1.1.1",
"items": null,
"componentStateId": 0,
"ctastatus": 0,
"actionId": "26",
"actionToPerform": "1"
},
{
"revision": 0,
"componentName": "tuv",
"componentIdentification": "tuv",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "210",
"rowId": "1.1.1.2",
"items": null,
"componentStateId": 0,
"ctastatus": 0,
"actionId": "5",
"actionToPerform": "1"
}
],
"componentStateId": 0,
"ctastatus": 0,
"actionId": "25",
"actionToPerform": "1"
}
],
"componentStateId": 0,
"ctastatus": 0,
"actionId": "1",
"actionToPerform": "1"
}
],
"componentStateId": 0,
"ctastatus": 0,
"actionId": "37",
"actionToPerform": "1"
},
{
"revision": 0,
"componentName": "wxy",
"componentIdentification": "wxy",
"componentType": "1",
"componentState": "1",
"componentUrl": null,
"componentId": "211",
"rowId": "2",
"items": [
{
"revision": 0,
"componentName": "zab",
"componentIdentification": "zab",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "212",
"rowId": "2.1",
"items": null,
"componentStateId": 0,
"ctastatus": 0,
"actionId": "7",
"actionToPerform": "1"
},
{
"revision": 0,
"componentName": "cde",
"componentIdentification": "cde",
"componentType": "2",
"componentState": "1",
"componentUrl": null,
"componentId": "213",
"rowId": "2.2",
"items": null,
"componentStateId": 0,
"ctastatus": 0,
"actionId": "12",
"actionToPerform": "1"
}
],
"componentStateId": 0,
"ctastatus": 0,
"actionId": "37",
"actionToPerform": "1"
}
]
现在,父子关系存在于具有items[]
array的一行和另一行之间。所以我试着写下面的代码,但它不起作用
方法调用:-
var newArray = $scope.isRowIdExist($scope.componentList,rowId);
方法
$scope.isRowIdExist = function(list,rowId) {
var newArray = _.filter(list, function(arrayItem) {
if(rowId != arrayItem.rowId){
if (arrayItem.items && arrayItem.items.length > 0){
$scope.isRowIdExist(arrayItem.items,rowId); // method calling itself
}
}
return rowId !== arrayItem.rowId;
});
}
我建议在这种情况下避免过滤。此代码用于删除父级或子级:
function removeRow(items, rowId) {
for (var i=0; i<items.length; i++) {
var item = items[i];
if (rowId == item.rowId) {
items.splice(i, 1);
} else if (rowId.startsWith(item.rowId)) {
removeRow(item.items, rowId);
}
if (item.items != null && item.items.length == 0) item.items = null;
}
}
函数删除程序(项目,行ID){
对于(var i=0;i我建议在这种情况下避免过滤。此代码用于删除父项或子项:
function removeRow(items, rowId) {
for (var i=0; i<items.length; i++) {
var item = items[i];
if (rowId == item.rowId) {
items.splice(i, 1);
} else if (rowId.startsWith(item.rowId)) {
removeRow(item.items, rowId);
}
if (item.items != null && item.items.length == 0) item.items = null;
}
}
函数删除程序(项目,行ID){
对于(var i=0;i尝试此操作,将目标元素替换为“已删除”,将其从生产时的拼接功能中删除
angular.module(“myApp”,[])
.controller(“ctrl”,函数($scope){
$scope.data=JSON.parse('[{“修订版”:0,“组件名称”:“abc”,“组件标识”:“abc”,“组件类型”:“1”,“组件状态”:“1”,“组件URL”:“null”,“组件ID”:“214”,“rowId”:“3”,“项”:[{“修订版”:0,“组件名称”:“efg”,“组件标识”:“efg”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:“null”,“组件D”:“215”,“rowId:“3.1”,“items:”null,“componentStateId:”0,“ctastatus:”0,“actionId:”16”,“actionToPerform:”1“}],”componentStateId:”0,“actionId:”37”,“actionToPerform:”1“},“,”修订“:0,“componentName:”hij”,“componentIdentification:”1”,“componentState:”1”,“componentUrl:”null,“componentId:”206”,“rowId:”1”,“items:“{”修订版:0,“组件名称”:“klm”,“组件标识”:“klm”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:null,“组件ID”:“207”,“rowId”:“1.1”,“项”:[{“修订版”:0,“组件名称”:“nop”,“组件标识”:“nop”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:null,“组件D”:“208”,“rowId”:“1.1.1”,“项”:[{”修订版:0,“componentName”:“qrs”,“componentIdentification”:“qrs”,“componentType”:“2”,“componentState”:“1”,“componentUrl”:null,“componentId”:“209”,“rowId”:“1.1.1.1”,“items”:null,“componentStateId”:0,“actionId”:“26”,“actionToPerform”:“1”,{“修订版”:0,“componentName”:“tuv”,“componentIdentification”:“tuv”,“componentType”:“2”componentState:“1”,“componentUrl:“null”,componentId:“210”,“rowId:“1.1.1.2”,“items:”null,“componentStateId:”0,“ctastatus:”0,“actionId:”5,“actionToPerform:”1“}],“componentStateId:”0,“ctastatus:”0,“actionId:”1,“actionToPerform:”1“}],”0,“ctastatus:”0,“actionId:“37”,“actionToPerform:“1”},{“修订版”:0,“组件名称”:“wxy”,“组件标识”:“wxy”,“组件类型”:“1”,“组件状态”:“1”,“组件URL:”null,“组件ID:”211,“rowId:”2”,“条目”:[{“修订版”:0,“组件名称”:“zab”,“组件标识”:“zab”,“组件类型”:“2”,“组件状态”:“1”,“组件URL:”null,“组件D”“:“212”,“rowId:“2.1”,“items”:null,“componentStateId:”0,“ctastatus:”0,“actionId:”7,“actionToPerform:”1“},{“修订版”:0,“componentName:”cde”,“componentIdentification:”cde”,“componentType:”2”,“componentState:”1”,“componentUrl:”null,“componentId:”213,“rowId:”2.2,“items”:null,“componentStateId:”0,“ctastatus:”0,“actionId:”12”,“actionToPerform”:“1”}],“componentStateId”:0,“ctastatus”:0,“actionId”:“37”,“actionToPerform”:“1”}];
$scope.toDel=“1.1”
$scope.removelement=函数(数据、toDel、父级){
data.forEach(函数(第一行){
if(row.rowId==toDel){
如果(家长){
父项拼接(i,1,“移除”)
}否则{
数据拼接(i,1,“移除”)
}
}else if(行项目){
$scope.removeElement(行.items、toDel、行)
}
})
}
$scope.removelement($scope.data、$scope.toDel,null);
})
{{data}json}
尝试此操作,将带有的目标元件替换为“已移除”,将其从生产时的拼接功能中移除
angular.module(“myApp”,[])
.controller(“ctrl”,函数($scope){
$scope.data=JSON.parse('[{“修订版”:0,“组件名称”:“abc”,“组件标识”:“abc”,“组件类型”:“1”,“组件状态”:“1”,“组件URL”:“null”,“组件ID”:“214”,“rowId”:“3”,“项”:[{“修订版”:0,“组件名称”:“efg”,“组件标识”:“efg”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:“null”,“组件D”:“215”rowId:“3.1”,“items:”null,“componentStateId:”0,“ctastatus:”0,“actionId:”16,“actionToPerform:”1“}],”componentStateId:”0,“actionId:”37,“actionToPerform:”1“},“{”修订“:0,“componentName:”hij”,“componentIdentification:”1,“componentState:”1”,“componentUrl:”null,“componentId:”206,“rowId:”1”,“items:“{”修订版:0,“组件名称”:“klm”,“组件标识”:“klm”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:null,“组件ID”:“207”,“rowId”:“1.1”,“项”:[{“修订版”:0,“组件名称”:“nop”,“组件标识”:“nop”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:null,“组件D”:“208”,“rowId”:“1.1.1”,“项”:[{”修订版:0,“组件名称”:“qrs”,“组件标识”:“qrs”,“组件类型”:“2”,“组件状态”:“1”,“组件URL”:null,“组件ID”:“209”,“rowId”:“1.1.1”