I have a list from which I want to remove a row. The initial code is as follows:
var newArray = _.filter($scope.componentList, function(arrayItem) {
return rowId !== arrayItem.rowId;
});
$scope.componentList = newArray;
This filter function will keep the objects for which the return value is true and remove those for which it is false. Now, I want the same logic to be applied for both parent and child elements.
In this function, rowId
acts as the input parameter. In the line $scope.componentList = newArray;
, only the objects with matching rowId
values are retained in the list.
return rowId !== arrayItem.rowId;
The above line ensures that only the objects satisfying the condition are kept in the new array.
But now the format of the data has changed. It looks like this:
[
{
"revision": 0,
"componentName": "abc",
...
...
"actionToPerform": "1"
},
...
]
The relationship between parent and child rows is established using the items[]
array. An attempt was made to modify the code accordingly, but it did not work as expected.
Method call:-
var newArray = $scope.isRowIdExist($scope.componentList,rowId);
Method Definition:
$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); // recursive method call
}
}
return rowId !== arrayItem.rowId;
});
}