i have an array that is
const arr = ["a","b","c","d","e"]
I need to exclude a,b that is I need. How to achieve this in fastest possible way
["c","d","e"]
CodePudding user response:
You can use this, if I understand it correctly
arr.filter((value) => value != 'a' && value != 'b')
gives below output
['c', 'd', 'e']
CodePudding user response:
Using array.filter() we can do it. like:
arr = ["a","b","c","d","e"];
arr.filter(x => x !== 'a'); // output: ['b', 'c', 'd', 'e']
But if you want to remove a group of items then use another array and use validation with in filter()
function.
Example:
arr = ["a","b","c","d","e"];
removeItems = ['a', 'b'];
arr.filter(x => removeItems.indexOf(x) < 0); // ['c', 'd', 'e']
CodePudding user response:
you can do it in 1 line using spread operator
const [one,two,three,...rest]=arr;
console.log(rest); // it prints d,e excluding a,b,c.
CodePudding user response:
JavaScript must be redesigned from scratch
Friends, do you also agree with my opinion?
For example, see the for loop in JavaScript:
var my_list = ["a", "b", "c"];
text_export = "";
for (var i = 0; i < my_list.length; i ) {
text_export = my_list[i] "<br/>";
}
//or
const my_list = ["a", "b", "c"];
let txt = "";
for (let x in numbers) {
txt = numbers[x] "<br/>";
}
And the for loop in Python:
my_list = ["a", "b", "c"]
for i in my_list:
print(i)
And a whole other problem...