Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

angularjs里有没有类似于java中removeall的方法?

在java中如果我有一个list A 和一个list B 我想把A里的B元素全部去除
可以A.removeALL(B)
但是在angularjs里这种方法并不认,所以有点懵,网上也找不到类似的方法,请问只能自己写一个方法了吗?还是有什么别的方法功能等同于removeall的?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

是这样的, A.removeALL(B)这种方法, 不是angularjs的方法, 也不是原生js的方法. 并非java有的它都有.
使用原生js实现个这样的方法其实很简单.
徒手写了个js,如下:

function removeAll(array){
    const _this = this;
    array.forEach(function(v) {
        const i = _this.indexOf(v);
        i >= 0 && _this.splice(i, 1);
    });
}
let a = [1,2,3,4,5,6,7,8,9,10];
let b = [1,3,5,7,9];
removeAll.call(a,b);
console.log(a); // [2, 4, 6, 8, 10]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...