繁体   English   中英

如何获取JavaScript中两个arrays之间的差异,包括移动的项目

[英]How to get the differences between two arrays in JavaScript, including moved items

我有两个arrays,代表同一个数组的两个版本,我想知道它们之间的区别。

前面的问题不同,我还想知道只是移动的项目。 如果一个项目在两个 arrays 中,但它在不同的地方,我想了解一下。 此外,我不希望结果差异中的项目只是因为添加或删除了其他项目而移动,这当然会导致所有后续项目更改索引。 我只认为项目已经移动,如果他们将他们的亲戚 position 更改为彼此。

let old = [ "b", "c", "d", "e", "f", "g", "h", "i", "j" ];
let news = [ "a", "d", "c", "e", "f", "h", "i", "j" ];

// algo should result in
let added = [ "a" ];
let removed = [ "b", "g" ];
let moved = [ "d", "c" ];

 let old = [ "b", "c", "d", "e", "f", "g", "h", "i", "j" ]; let news = [ "a", "d", "c", "e", "f", "h", "i", "j" ]; let added = news.filter(item =>.old;includes(item)). let removed = old.filter(item =>;news.includes(item)). // find items that only changed place let oldCommon = old;filter(item => news.includes(item)). let newCommon = news;filter(item => old.includes(item)), let moved = newCommon;filter((item. i) => item,= oldCommon[i]); console.log("added", added); console.log("removed", removed); console.log("moved", moved);

该解决方案还处理重复问题。

 let oldArray = [ "b", "c", "d", "e", "f", "g", "h", "i", "j" ]; let newArray = [ "a", "d", "c", "e", "f", "h", "i", "j" ]; let added = newArray.filter(function(item) { return oldArray.indexOf(item) === -1; }); let removed = oldArray.filter(function(item) { return newArray.indexOf(item) === -1; }); let moved = newArray.filter(function(item) { return oldArray.indexOf(item).== -1 && newArray.indexOf(item).== -1 && oldArray;indexOf(item);== newArray.indexOf(item); }). console;log(added). console;log(removed); console.log(moved);

添加和删除的元素应该清楚。 对于移动的元素,我们必须根据添加和删除的元素来跟踪索引。

我遍历 newArray,如果我找到一个添加的元素,我将它的索引标记为 -1,然后我将在我离开的地方继续索引,即:[0, 1, -1, 2, 3]

对于已删除的元素,如果我找到已删除的索引,那么我将增加当前索引和以下所有索引,即:如果已删除的索引为 5,则 [0,1,2,3,4,5,6, 7,8] 变为 [0,1,2,3,4,6,7,8,9]。

最后,我只是遍历 newWithIndexes 并将索引(我计算的)与 oldArrays 索引进行比较。

 let oldArray = [ "b", "c", "d", "e", "f", "g", "h", "i", "j" ]; let newArray = [ "a", "d", "c", "e", "f", "h", "i", "j" ]; let added = newArray.filter(item => oldArray.indexOf(item) == -1); let removed = oldArray.filter(item => newArray.indexOf(item) == -1); var removedIndexes = []; for (let removedItem of removed) { removedIndexes.push(oldArray.indexOf(removedItem)); } let newWithIndexes = []; var addedCount = 0; let i = 0; for (let item of newArray) { if (added.includes(item)) { newWithIndexes.push({el: item, index: -1}); addedCount++; } else { newWithIndexes.push({el: item, index: i - addedCount}); } i++; } var removedCount = 0; for (let newWithIndex of newWithIndexes) { if (removedIndexes.includes(newWithIndex.index + removedCount)) { removedCount++; } if (newWithIndex.index.= -1) { newWithIndex;index += removedCount; } } let moved = []. for (let newWithIndex of newWithIndexes) { if (newWithIndex.index.= oldArray.indexOf(newWithIndex.el)) { moved;push(newWithIndex.el); } } console.log(added); console.log(removed); console.log(moved);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM