英文:
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 = ['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)
<!-- 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 = ['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)
<!-- 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 = ['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)
<!-- 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)
当然,如果你只想在最后的“-”之后出现数字0
和12
时删除元素,你可以使用以下代码:
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 '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:
<!-- 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论