1

I have this array

var array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12']

and I would like to remove elements that contain "-12" and "-0"

Expected Result = ['20-2', '319-2', '161-2', '320-2']

How can I achieve this expected result in javascript.

asked Feb 10, 2023 at 5:33

4 Answers 4

3

You can do this with .filter.

Example:

var array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12'];
var filtered = array.filter(item => !item.includes('-12') && !item.includes('-0'));

After executing this code, filtered becomes ['20-2', '319-2', '161-2', '320-2'].

answered Feb 10, 2023 at 5:37
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Array.filter method in combination with String.includes method.
Here is an example:

let array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12']
let newArray = array.filter((element) =>
 !element.includes('-12') && !element.includes('-0')
)
console.log(newArray)

answered Feb 10, 2023 at 5:40

Comments

1

This can be achieved using the Array.filter combined with String.includes methods. The Array.filter method creates a new Array with each item that results in a positive condition.

let array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12']
let filteredArray = array.filter(item => (!item.includes("-12")) && !item.includes("-0"))
console.log(filteredArray)

Reference:

answered Feb 10, 2023 at 5:40

Comments

0

If you want to find "12" and "0" at any position in the string (being the first or the second number) you can use a regular expression with \b symbolising a word boundary:

var array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12','3-120','112-14','12-7'];
const res = array.filter(el=>![/\b0\b/,/\b12\b/].some(rx=>rx.test(el)));
console.log(res)

The word boundaries in the rx ensure that elements like '3-120','112-14' will not be removed from the result set.

Instead of using two regular expressions you can - for the given problem - combine the search requirements into one:

var array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12','3-120','112-14','12-7'];
const res = array.filter(el=>!/\b(?:0|12)\b/.test(el));
console.log(res)

And, of course, if you only want to remove the elements when the numbers 0 and 12 appear after the (last) "-", you can do the following:

var array = ['20-2', '319-2', '161-2', '320-2', '12-0', '575-12', '279-12', '280-12', '412-12', '423-12', '424-12', '425-12', '291-12', '0-12', '449-12','3-120','112-14','12-7'];
const res = array.filter(el=>!/-(?:0|12)$/.test(el));
console.log(res)

answered Feb 10, 2023 at 5:41

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.