我有一个动物数组 arr = ['cat','dog','elephant','lion','tiger','mouse']
我想写一个函数remove(['dog','lion'])
,它可以从arr中删除元素,我们能否使用es6展开运算符编写此函数?
示例:
arr = ['cat','dog','elephant','lion','tiger','mouse']
remove(['cat', 'lion'])
arr应该被修改为
arr = ['dog','elephant','tiger','mouse']
注意:我不希望进行改变,因此请不要提出会改变数组的解决方案。
function remove(x, y) { return x.length == 0 ? x : y.includes(x[0]) ? remove(x.slice(1), y) : [x[0], ...remove(x.slice(1), y)]; }
- Bergi