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
滿天的星座2017-06-26 10:54:07
Do you want to compare or merge? Deepmerge is very convenient for merging
学习ing2017-06-26 10:54:07
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
给我你的怀抱2017-06-26 10:54:07
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
typecho2017-06-26 10:54:07
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)
漂亮男人2017-06-26 10:54:07
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"}]);