This question already has answers here :
The v
in flatsArray.some
callback function returns a single item rather than an array of items. So rather than checking for the index of, you can simply compare the values directly.
You need to
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return !$scope.someObj.flatsArray.some(function(v){
return v == f;
})
})
Remove all elements contained in another array, Use the Array.filter() method: myArray = myArray.filter( function( el ) { return toRemove.indexOf( el ) < 0; } );. Small improvement, as browser support for Filtering out an array of objects based on an array of values in a react component: const filteredResults = this.state.cards.filter( result => !this.state.filterOut.includes(result.category) ) where this.state.cards in an array of objects and this.state.filterOut is an array of values that correspond the 'category' key in the objects that I wanted to remove.
Pick either some
or indexOf
. For example
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return $scope.someObj.flatsArray.indexOf(f) === -1
})
or
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return !$scope.someObj.flatsArray.some(function(item) { return item === f; })
})
How to filter an array with another array, There are two scenarios I would like to understand better. (2) The second array loses the elements that it used to filter the first array. you gave for example two, does false trigger filter to remove 'b' and 'd' from array2? The filter function can be used along with lambda to perform this task and creating a new filtered list of all the elements that are not present in the remove element list. filter_none edit
It might be helpful to post the arrays so they can be used in answers, however, you could do this.
for (var i = 0; i < $scope.userEventData.selectedFlats; i++) {
var index = $scope.flatsArray.indexOf($scope.userEventData.selectedFlats[i]);
if ( index > -1 ) {
$scope.userEventData.selectedFlats.splice(index, 1);
}
}
This will loop through each item in the selectFlats array and find the index of that item in the flatsArray , then remove that item from if it exists.
How do I remove from an array those elements that exists in another , It needs to be just JavaScript; no lodash or underscore. Using map or filter is fine. Thank you. 11 comments. share. Stop Array.forEach and start using filter, map, some, reduce functions. all the elements of an array to get a single value out of it. is another flavor of Array.reduce which traverses
Try this:
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(
function(item) {
return !($scope.someObj.flatsArray.contains(item))
}
JavaScript, In order to remove empty elements from an array, filter() method is used. the elements of one array which are not present in another array using JavaScript? To filter a data in an array formula (to exclude or require certain values), you can use an array formula based on the IF, MATCH, and ISNUMBER functions. where "data" is the named range B4:D11 and "filter" is the named range F4:F6. Note: this is an array formula and must be entered with control + shift + enter.
Array methods, In the next example we remove 3 elements and replace them with the other two: let arr = [ "I" The function is called for elements of the array, one after another: item is the If there may be many, we can use arr.filter(fn). Array filtering is one of those rarely used items that also saves buckets of time and many lines of codes when it is actually applicable. In my case I want the script to accept "a list of things to include" but to successfully configure the target item, I must turn it into a "list of things to exclude".
Filter array with another array, This is little neater IMO. // User can be part of many groups const user = { groups: ["group2"] }; // Roles can have many groups // What you see Now if we pass this bool array to [] operator of numpy array arr, then it will select the elements from arr foe which bool array has True at corresponding index. Basically it returns the elements from arr which are not 6. Another point to be noted is that it returns a copy of existing array with elements with value 6.
array_filter - Manual, array_filter — Filters elements of an array using a callback function If the callback function returns TRUE , the current value from array is returned into the result array. Array This function filters an array and remove all null values recursively. Remove empty elements from an array in PowerShell I will demonstrate how to remove / filter out empty elements from an array in PowerShell. with the numeric
Comments you should console.log(v)
to check what you are getting there ooops..i totally forgot A fully qualified ===
should always be used unless you specifically intend for a loose match to be made (and then documented as such)