2

I have this array:

["onelolone", "twololtwo", "three"]

How can I remove the elements using only 'lol' as value and as result be:

["three"]
3

2 Answers 2

6

Use Array.prototype.filter, and check that it doesn't include "lol":

const result = ["onelolone","twololtwo","three"].filter(ele => !ele.includes("lol"))

console.log(result)// ["three"]

Sign up to request clarification or add additional context in comments.

2 Comments

Nice exactly what I been looking for. I have a question, if I want to include more values like ("lol","th") and expect = console.log(result)// [ ]. How can that be achieved?
.filter(ele => !["lol", "th"].some(phrase => ele.includes(phrase))
0

Array#filter is used to keep elements that satisfy a predicate. But we can build a reject function that removes the elements that satisfy a predicate:

const reject = fn => xs => xs.filter(x => fn(x) === false);

Then we can build a curried function for matching a string:

const matches = x => y => y.includes(x);
const reject_lol = reject(matches("lol"));

reject_lol(["onelolone", "twololtwo", "three"]);
//=> ["three"]

We can tweak matches to support any number of strings:

const matches = (...xs) => y => xs.some(x => y.includes(x));
const reject_abbr = reject(matches("lol", "wtf"));

reject_abbr(["lolone", "twowtf", "three"]);
//=> ["three"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.