Javascript 通过拼接将阵列替换为另一个阵列&;在不接触非唯一值的情况下推送
我有两个阵列:Javascript 通过拼接将阵列替换为另一个阵列&;在不接触非唯一值的情况下推送,javascript,arrays,push,splice,Javascript,Arrays,Push,Splice,我有两个阵列: var current_items = [{hash: 1}, {hash: 2}, {hash: 3}]; var next_items = [{hash: 3}, {hash: 4}, {hash: 5}]; 第一个数组表示当前渲染的项,另一个表示下一个要渲染的项(当然简化了-真实对象包含更多信息) 我需要用新项目替换当前\u项目数组,只需按/拼接即可。问题是,我不能仅仅用新项目覆盖当前项目:所有与下一个项目具有相同哈希的当前项目必须保持原样(如果对象被更改/覆盖,
var current_items = [{hash: 1}, {hash: 2}, {hash: 3}];
var next_items = [{hash: 3}, {hash: 4}, {hash: 5}];
第一个数组表示当前渲染的项,另一个表示下一个要渲染的项(当然简化了-真实对象包含更多信息)
我需要用新项目替换当前\u项目
数组,只需按/拼接即可。问题是,我不能仅仅用新项目覆盖当前项目:所有与下一个项目具有相同哈希的当前项目必须保持原样(如果对象被更改/覆盖,则项目将不必要地再次呈现)
我猜第一步是删除/拼接当前\u项
中未包含在下一个\u项中的所有项:当前\u项将成为[{hash:3}]
(保留hash 3,因为它也包含在下一个\u项中)
然后从下一个\u项
中删除已包含在当前\u项
中的所有项,使下一个\u项
变成[{hash:4},{hash:5}]
最后,将剩余的下一个\u项
与当前\u项
current_items.push.apply(current_items, next_items);
结果是[{hash:3},{hash:4},{hash:5}]
我当前的解决方案如下所示:
var current_items = [{hash: 1}, {hash: 2}, {hash: 3}];
var next_items = [{hash: 3}, {hash: 4}, {hash: 5}];
// splice old items
var i = current_items.length, j, found;
while (i--) {
found = false;
for (j = 0; j < next_items.length; j++) {
if (current_items[i]['hash'] === next_items[j]['hash']) {
found = true;
break;
}
}
!found && current_items.splice(i, 1);
}
// get unique new items
var unique_new_items = [];
for (i = 0; i < next_items.length; i++) {
found = false;
for (j = 0; j < current_items.length; j++) {
if (next_items[i]['hash'] === current_items[j]['hash']) {
found = true;
break;
}
}
!found && unique_new_items.push(next_items[i]);
}
current_items.push.apply(current_items, unique_new_items);
// [{hash: 3}, {hash: 4}, {hash: 5}]
var current_items=[{hash:1},{hash:2},{hash:3}];
var next_items=[{hash:3},{hash:4},{hash:5}];
//拼接旧项目
var i=当前_项目。长度,j,已找到;
而(我--){
发现=错误;
对于(j=0;j
有更简单/更干净/更短的方法吗?使用
Array.filter()
,Array.concat()
,Array.splice()
,JSON.stringify()
和JSON.parse()函数的解决方案:
var current_items=[{hash:1},{hash:2},{hash:3}],
next_items=[{hash:3},{hash:4},{hash:5}],
next\u items\u stringify=next\u items.map(JSON.stringify);
当前项目=当前项目。过滤器(函数(o){
var pos=this.indexOf(JSON.stringify(o));
返回位置!=-1和该拼接(位置1);
}concat(next_items_stringified.map(JSON.parse));
console.log(当前_项)代码>您可以使用两个哈希表进行哈希运算,并相应地拼接。稍后将未包含的对象推送到当前\u项中
var current_items=[{hash:1},{hash:2},{hash:3,keep:true/*标识此对象的标志*/}],
next_items=[{hash:3},{hash:4},{hash:5}],
hashNext=Object.create(null),
hashCurrent=Object.create(null);
下一步\u items.forEach(函数(a){
hashNext[a.hash]=true;
});
当前\u项。还原右(函数(\u、a、i、aa){
hashCurrent[a.hash]=true;
如果(!hashNext[a.hash]){
aa.拼接(i,1);
}
}, 0);
下一步\u items.forEach(函数(a){
如果(!hashCurrent[a.hash]){
当前项目推送(a);
}
});
console.log(当前_项)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以执行以下操作
var current_items=[{hash:1},{hash:2},{hash:3},{hash:4}],
下一个_items=[{hash:3},{hash:4},{hash:5},{hash:6}],
结果=当前\u项。筛选器(h=>下一个\u项。某些(g=>g.hash==h.hash))
.reduce((p,c,i,a)=>i?p.concat(next_items.filter(n=>p.every(e=>e.hash!==n.hash)))
:p.concat(a,next_items.filter(n=>a.every(e=>e.hash!==n.hash)),[];
控制台日志(结果)代码>只要数组是基于散列排序的,就可以执行以下操作(检查两次,我不能100%确定它是否适用于每个配置)
var curr=[{hash:1},{hash:2},{hash:3}];
var next=[{hash:3},{hash:4},{hash:5}];
var i=0;
while(next.length>0 | | inext[0].hash){
当前拼接(i++,0,next.shift());
}else if(curr[i].hashx.hash).join(“”,”);
console.log(“[”,next.map(x=>x.hash).join(“”,“])
您知道js不保证属性顺序,因此通过indexOf
&&JSON.stringify
进行过滤可能并不总是有效的?