2

I have this array:

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

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

["three"]
Aplet123
35.8k1 gold badge41 silver badges66 bronze badges
asked Jan 4, 2021 at 0:01
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"]

answered Jan 4, 2021 at 0:03
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"]
answered Jan 4, 2021 at 0:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.