首页 > 解决方案 > 当我们必须删除另一个数组中列出的元素时,我不知道该怎么做

问题描述

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]

标签: javascriptarrays

解决方案


要删除数组中的某些内容,建议使用.filter

const input = [1, 2, 2, 3, 1, 2];
const without = [2, 3];

const result = input.filter(value => !without.includes(value))
console.log(result)


推荐阅读