Mhd
Mhd

Reputation: 811

How to remove specific array elements in javascript?

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.

Upvotes: 1

Views: 113

Answers (4)

Stefan Walter
Stefan Walter

Reputation: 126

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:

Upvotes: 1

Carsten Massmann
Carsten Massmann

Reputation: 28196

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)

Upvotes: 0

emilianscheel
emilianscheel

Reputation: 36

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)

Upvotes: 2

Joshua F
Joshua F

Reputation: 130

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'].

Upvotes: 3

Related Questions