如何使用ES6 .filter()
的{{1}}从另一个数组列表中的数组列表中减去/删除/删除所有重合元素?
Jquery的解决方案也被接受,但任何解决方案必须是IE11 +和Chrome iOs兼容。避免使用外部库(jquery除外)。这是我到目前为止所尝试的:
javascript
var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
var newList = [];
var list = [];
$.each(originList, function(id,data){
list = originList.filter(function(obj,a){
return data.id !== obj.id ? obj : false;
});
});
$.each(originList, function(id,data){
$.each(rest, function(alli,allData){
if(data.id === allData.id) {
newList[id] = data;
}
});
});
console.log(list); //does not exclude id:6
console.log(newList); //this result is what I want
答案 0 :(得分:3)
您可以将find
和var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
var newList = originList.filter( o => rest.find( x => o.id === x.id ) );
console.log( newList );
用于较新版本。
for
可以使用if
和var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
var newList = [];
for (var i = 0; i < originList.length; i++) {
for (var x = 0; x < rest.length; x++) {
if ( rest[x].id === originList[i].id ) {
newList.push( originList[i] );
break;
}
}
}
console.log( newList );
用于旧版本。
Winforms
答案 1 :(得分:0)
你可以试试这个
list3
答案 2 :(得分:0)
如果你有lodash,你可以使用函数_.differenceWith
并传递_.isEqual
作为每个元素调用的比较器(如果你想要完全相等)或自定义比较器,如果你只想比较对象的一些属性以确定相等。
Lodash还授予您浏览器兼容性。
希望它有所帮助。
var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
console.log(_.differenceWith(originList, rest, _.isEqual));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
答案 3 :(得分:0)
var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
const newList = originList.filter(function(originListVal) {
return rest.reduce(function(currentVal, nextRestVal) {
return currentVal || originListVal.id === nextRestVal.id;
}, false);
});
console.log(newList);
&#13;
答案 4 :(得分:0)
var originList = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
{"id":6, "name":"Susane"},
{"id":7,"name":"yyy"}
];
var rest = [
{"id":1,"name":"www"},
{"id":2, "name":"fff"},
{"id":3, "name":"ddd"},
{"id":4, "name":"aaaa"},
{"id":5, "name":"zzzz"},
];
function containsObject(obj, list) {
for (var i = 0; i < list.length; i++) {
if (list[i].id === obj.id) return true;
}
return false;
}
var newList = originList.filter(function(item){ return !containsObject(item, rest)
});
console.log( newList );
&#13;
答案 5 :(得分:0)
let newList = originList.filter(ele => !rest.some(fil => ele.id === fil.id));
如果不能使用箭头功能
let newList = originList.filter(function(ele) {
return rest.some(function(fil) {
return ele.id === fil.id;
});
});