如何在 JavaScript 中删除特定的数组元素?

huangapple go评论54阅读模式
英文:

How to remove specific array elements in javascript?

问题

我有这个数组

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

我想要删除包含"-12"和"-0"的元素

期望的结果 = ['20-2', '319-2', '161-2', '320-2']

在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.

答案1

得分: 2

你可以使用.filter来实现这个。

示例:

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'));

执行这段代码后,filtered变为['20-2', '319-2', '161-2', '320-2']

英文:

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

答案2

得分: 2

你可以使用 Array.filter 方法与 String.includes 方法结合使用。
<br/>
这里有一个例子:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let array = [&#39;20-2&#39;, &#39;319-2&#39;, &#39;161-2&#39;, &#39;320-2&#39;, &#39;12-0&#39;, &#39;575-12&#39;, &#39;279-12&#39;, &#39;280-12&#39;, &#39;412-12&#39;, &#39;423-12&#39;, &#39;424-12&#39;, &#39;425-12&#39;, &#39;291-12&#39;, &#39;0-12&#39;, &#39;449-12&#39;]


let newArray = array.filter((element) =&gt;
        !element.includes(&#39;-12&#39;) &amp;&amp; !element.includes(&#39;-0&#39;)
)

console.log(newArray)

<!-- end snippet -->

英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let array = [&#39;20-2&#39;, &#39;319-2&#39;, &#39;161-2&#39;, &#39;320-2&#39;, &#39;12-0&#39;, &#39;575-12&#39;, &#39;279-12&#39;, &#39;280-12&#39;, &#39;412-12&#39;, &#39;423-12&#39;, &#39;424-12&#39;, &#39;425-12&#39;, &#39;291-12&#39;, &#39;0-12&#39;, &#39;449-12&#39;]


let newArray = array.filter((element) =&gt;
        !element.includes(&#39;-12&#39;) &amp;&amp; !element.includes(&#39;-0&#39;)
)

console.log(newArray)

<!-- end snippet -->

答案3

得分: 1

这可以通过使用Array.filter结合String.includes方法来实现。Array.filter方法会创建一个新数组,其中包含满足特定条件的每个项。

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)

参考:

英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let array = [&#39;20-2&#39;, &#39;319-2&#39;, &#39;161-2&#39;, &#39;320-2&#39;, &#39;12-0&#39;, &#39;575-12&#39;, &#39;279-12&#39;, &#39;280-12&#39;, &#39;412-12&#39;, &#39;423-12&#39;, &#39;424-12&#39;, &#39;425-12&#39;, &#39;291-12&#39;, &#39;0-12&#39;, &#39;449-12&#39;]

let filteredArray = array.filter(item =&gt; (!item.includes(&quot;-12&quot;)) &amp;&amp; !item.includes(&quot;-0&quot;))

console.log(filteredArray)

<!-- end snippet -->

Reference:

答案4

得分: 0

如果你想在字符串的任何位置(无论是第一个还是第二个数字)找到“12”和“0”,你可以使用正则表达式,并使用\b表示单词边界:

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)

rx中的单词边界确保像'3-120','112-14'这样的元素不会从结果集中删除。

而不是使用两个正则表达式,你可以将搜索要求组合成一个:

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)

当然,如果你只想在最后的“-”之后出现数字012时删除元素,你可以使用以下代码:

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)
英文:

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:

<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
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)
<!-- end snippet -->

The word boundaries in the rx ensure that elements like &#39;3-120&#39;,&#39;112-14&#39; 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:

<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
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)
<!-- end snippet -->

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:

<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
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)
<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 13:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定