javascript - 当我们必须删除另一个数组中列出的元素时,我不知道该怎么做
问题描述
function removeListedValues(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
arr
: 给定的数组
without
:要从 arr 中删除的元素列表。删除列出的值后返回数组。
输入:arr:[1, 2, 2, 3, 1, 2]
没有:[2, 3]
输出:
[1, 1]
解决方案
要删除数组中的某些内容,建议使用.filter
const input = [1, 2, 2, 3, 1, 2];
const without = [2, 3];
const result = input.filter(value => !without.includes(value))
console.log(result)
推荐阅读
- php - 达到均值、中值和众数时 PHP 崩溃
- c++ - 在代码末尾注释掉 cin 会更改先前使用的 cout 语句的输出。为什么会这样?
- vue.js - Nuxt 返回带有异步数据的 sitemap.xml 路由
- autodesk-forge - 在作业的 CoreEngineExecution 阶段发生意外错误
- laravel - Laravel:在where子句中使用count抛出错误
- python - 函数在大列表大小时失败
- javascript - 使用 Jquery 从 HTML 文本中删除“”(无空格)
- javascript - (Discord.JS)我如何听取作者选择的特定用户的用户提及
- python - 如何从字符串中某些行的末尾删除 4 个字符?
- javascript - 从输入字段中获取位置并将其传递到 MapBox 地图上