What is the easiest way to remove all elements from array that match specific string? For example:
array = [1,2,'deleted',4,5,'deleted',6,7];
I want to remove all 'deleted'
from the array.
Simply use the Array.prototype.filter() function for obtain elements of a condition
var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})
let array = [1,2,'deleted',4,5,'deleted',6,7]
let newarr = array.filter(a => a !== 'deleted')
If you have multiple strings to remove from main array, You can try this
// Your main array
var arr = [ '8','abc','b','c'];
// This array contains strings that needs to be removed from main array
var removeStr = [ 'abc' , '8'];
arr = arr.filter(function(val){
return (removeStr.indexOf(val) == -1 ? true : false)
})
console.log(arr);
// 'arr' Outputs to :
[ 'b', 'c' ]
OR
Better Performance(Using hash) , If strict type equality not required
// Your main array
var arr = [ '8','deleted','b','c'];
// This array contains strings that needs to be removed from main array
var removeStr = [ 'deleted' , '8'];
var removeObj = {}; // Use of hash will boost performance for larger arrays
removeStr.forEach( e => removeObj[e] = true);
var res = arr.filter(function(val){
return !removeObj[val]
})
console.log(res);
// 'arr' Outputs to :
[ 'b', 'c' ]
array = array.filter(function(s) {
return s !== 'deleted';
});
If you want the same array then you can use
var array = [1,2,'deleted',4,5,'deleted',6,7];
var index = "deleted";
for(var i = array.length - 1; i >= 0; i--) {
if(array[i] === index) {
array.splice(i, 1);
}
}
else you can use Array.prototype.filter
which creates a new array with all elements that pass the test implemented by the provided function.
var arrayVal = [1,2,'deleted',4,5,'deleted',6,7];
function filterVal(value) {
return value !== 'deleted';
}
var filtered = arrayVal.filter(filterVal);
A canonical answer would probably look like this:
[10, 'deleted', 20, 'deleted'].filter(x => x !== 'deleted');
//=> [10, 20]
There's nothing unexpected here; any developers can read, understand and maintain this code. From that perspective this solution is great. I just want to offer some different perspectives.
Firstly I sometimes struggle with the semantic of filter when the condition is "reversed":
[2, 3, 2, 3].filter(x => x === 2);
[2, 3, 2, 3].filter(x => x !== 2);
This is a contrived example but I bet a few readers did pause for a nanosecond. These small cognitive bumps can be exhausting in the long run.
I personally wish there would be a reject method:
[2, 3, 2, 3].filter(x => x === 2);
[2, 3, 2, 3].reject(x => x === 2);
Secondly there's a lot of "machinery" in this expression x => x === 2
: a function expression, a parameter and an equality check.
This could be abstracted away by using a curried function:
const eq =
x => y =>
x === y;
[2, 3, 2, 3].filter(eq(2));
//=> [2, 2]
We can see that eq(2)
is the same as x => x === 2
just shorter and with added semantic.
Now let's build a reject
function and use eq
:
const reject =
(pred, xs) =>
xs.filter(x =>
pred(x) === false);
reject(eq(2), [2, 3, 2, 3]);
//=> [3, 3]
But what if we need to reject other things? Well we can build an either
function that uses eq
:
const either =
(...xs) => y =>
xs.some(eq(y));
reject(either(1, 2), [1, 3, 2, 3]);
//=> [3, 3]
Finally to answer your question:
reject(eq('deleted'), [1, 'deleted', 3]);
//=> [1, 3]
reject(either('deleted', 'removed'), [1, 'deleted', 3, 'removed', 5]);
//=> [1, 3, 5]
We could go further and remove based on different predicates eg remove if matches the string "delete" or is 0 .
Let's build a eitherfn
function that takes a list of predicates:
const eitherfn =
(...fn) => y =>
fn.some(f =>
f(y));
And now let's build a match
function:
const match =
x => y =>
typeof y === 'string'
? y.includes(x)
: false;
Then:
reject(eitherfn(match('delete'), eq(0)), [0, 1, 'deleted', 3, 'will delete', 5])
// [1, 3, 5]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.