How to compare two arrays in javascript
習慣沉默
習慣沉默 2017-06-26 10:52:07
0
6
860

Array a

[{key:1,name:'name1'},{key:2,name:'name2'},{key:3,name:'name3'},{key:4,name:'name4'},{key:5,name:'name5'}]

Array b is part of array a

[{key:1,name:'name1'},{key:2,name:'name2'}]

Array c is modified based on array b

[{key:1,name:'name11'},{key:22,name:'name22'}]

Now the final result is needed: key:1 is modified, key:2 is deleted, key:3, key:4, key:5 are retained without change, key:22 is added

[{key:1,name:'name11'},{key:3,name:'name3'},{key:4,name:'name4'},{key:5,name:'name5'},{key:22,name:'name22'}]

Please tell me how to implement this logic

習慣沉默
習慣沉默

reply all(6)
滿天的星座

Do you want to compare or merge? Deepmerge is very convenient for merging

Ty80
//引入Lodash
_.extend(_.difference(a, b), c)
学习ing
var a = [{key:1,name:'name1'},{key:2,name:'name2'},{key:3,name:'name3'},{key:4,name:'name4'},{key:5,name:'name5'}];
var b = [{key:1,name:'name1'},{key:2,name:'name2'}];
var c = [{key:1,name:'name11'},{key:22,name:'name22'}];
var d = [];
d = $.extend(a,(b,c));
console.log(d);

The extend method is similar to git’s merge
Writing it like that should get the result you want in the end

给我你的怀抱
function compare(val1,val2){
    if(val1 < val2){
        return 1;
    }else if(val1 > val2){
        return -1;
    }else{
        return 0;
    }
}

var  values = [1,2,50,60,10,20,80];
values.sort(compare);
alert(values); //80,60,50,20,10,2,1
typecho

If you can’t find a good tool, or don’t want to pollute the namespace, then decouple the array, which should meet your requirements

let a = [{ key: 1, name: 'name1' }, { key: 3, name: 'name3' }, { key: 2, name: 'name2' }, { key: 4, name: 'name4' }, { key: 5, name: 'name5' }];
let b = [{ key: 1, name: 'name1' }, { key: 2, name: 'name2' }];
let c = [{ key: 22, name: 'name22' }, { key: 1, name: 'name11' }];
let akeys = a.map(x => x.key);
let aname = a.map(x => x.name)
let bkeys = b.map(x => x.key)//1,2
let bname = b.map(x => x.name)
let ckeys = c.map(x => x.key)//1,22
let cname = c.map(x => x.name)

ckeys.forEach(function (key, index) {
  if (bkeys.includes(key))
    a[akeys.indexOf(key)].name = cname[ckeys.indexOf(key)]
  else {
    a[akeys.indexOf(bkeys[1])].key = key
    a[akeys.indexOf(bkeys[1])].name = cname[ckeys.indexOf(key)]
  }
})
console.log(a)
漂亮男人

I’ll give you a piece of code to imagine (only suitable for merging with arrays). Just take the following code to the console and output it

function arrayExtends(){
    var arrs = Array.prototype.slice.call(arguments);

    if(arrs.length < 2){
        return arrs[0];
    }

    var save = arrs.pop(),
        now;

    while(now = arrs.pop()){
        for( var i = 0 , len = save.length; i < len; i++ ){
            now[i] = save[i];
        }
        save = now;
    }

       return save;
}
arrayExtends([1,2,3,6,7],[4,5,{hello:"world"}],[{key:1,name:"zz"}]);

Then modify it to suit your needs:

function arrayExtends(){
    var arrs = Array.prototype.slice.call(arguments);

    if(arrs.length < 2){
        return arrs[0];
    }

    var save = arrs.pop(),
        now;

    while(now = arrs.pop()){
        for( var i = 0 , len = save.length; i < len; i++ ){
            now[i] = save[i];
        }
        save = now;
    }

    var result = save.sort(function(a,b){
        return a.key - b.key;
    });

       return result;
}
arrayExtends([{key:1,name:"zz"}],[{key:2,name:"zz"},{key:3,name:"zz"}]);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template