我有以下陣列:
const prev = [A, C, D, E]
const curr = [A, B, D, F]
產生以下輸出的最優雅的方式是什么?
[[A, A], [null, B], [C, null], [D, D], [E, null], [null, F]]
A、B、C、.. 是可以映射到相等欄位的物件,例如:
A = {id: "A", ... }
最終陣列的順序無關緊要。
編輯:這兩個陣列不一定是相同的長度。
uj5u.com熱心網友回復:
這是我認為易于理解的解決方案(但可能不是最快或最短的)。
- 首先,組合陣列 (
concat) - 雖然新串列中有一些元素:
- 獲取第一個元素
- 使用給定的比較器查找比較器認為相同的另一個元素的索引
- 如果找到索引,則使用索引獲取元素并將其從串列中洗掉
- 否則,它是
null
- 否則,它是
- 將第一個元素和另一個元素添加到結果中
它似乎有效,如果您想更改壓縮輸出的標準,您可以更改比較器。
const prev = ["A", "C", "D", "E"];
const curr = ["A", "B", "D", "F"];
const comparator = (a, b) => a === b; // a.id === b.id
const zip = (a, b, comparator) => {
const result = [];
const list = a.concat(b);
while (list.length) {
const element = list.shift();
const pairIndex = list.findIndex((x) => comparator(x, element));
const pair = pairIndex >= 0 ? list.splice(pairIndex, 1)[0] : null;
result.push([element, pair]);
}
return result;
};
console.log(zip(prev, curr, comparator));
uj5u.com熱心網友回復:
我會使用這個演算法
const prev = [{ id: "A" }, { id: "C" }, { id: "D" }, { id: "E" }]
const curr = [{ id: "A" }, { id: "B" }, { id: "D" }, { id: "F" }]
const res = [];
const charCodeA = 'A'.charCodeAt(0);
let prevIndex = 0, currIndex = 0;
while (prevIndex < prev.length || currIndex < curr.length) {
const left = prev[prevIndex]?.id.charCodeAt(0) === res.length charCodeA ?
prev[prevIndex] : null;
const right = curr[currIndex]?.id.charCodeAt(0) === res.length charCodeA ?
curr[currIndex] : null;
if (left) {
prevIndex ;
}
if (right) {
currIndex ;
}
res.push([left, right])
}
console.log(res)
uj5u.com熱心網友回復:
使用集合運算來查找兩個陣列之間共有的所有值,僅在第一個陣列中,并且僅在第二個陣列中。然后為每個組創建適當的配對。
const prev = ['A', 'C', 'D', 'E'];
const curr = ['A', 'B', 'D', 'F'];
const prevSet = new Set(prev);
const currSet = new Set(curr);
const intersection = prev.filter(el => currSet.has(el));
const only_prev = prev.filter(el => !currSet.has(el));
const only_curr = curr.filter(el => !prevSet.has(el));
let result = intersection.map(el => [el, el]);
result = result.concat(only_prev.map(el => [el, null]));
result = result.concat(only_curr.map(el => [null, el]));
console.log(result);
uj5u.com熱心網友回復:
迭代第一個并填充 hash value => [value,null]。迭代第二個,如果 value 在 hash 中,則替換null為 value,否則添加[null, value]:
const prev = ['A','C','D','E']
const curr = ['A','B','D','F']
let m = {}
for (let a of prev)
m[a] = [a, null]
for (let a of curr)
if (a in m)
m[a][1] = a
else
m[a] = [null, a]
console.log(...Object.values(m))
uj5u.com熱心網友回復:
也許嘗試像這樣更通用的東西:
function zip(lists = []) {
const joined = lists.flatMap(list => list); // Put all elements in a single list
const set = new Set(joined); // Create a set to only have distinct elements
const items = [...set]; // Convert the set to a list
items.sort() // Sort the list
const result = []; // Create the result array
items.forEach(item => {
const zippedItem = []; // Create the zip line
lists.forEach(list => {
if (list.includes(item)) zippedItem.push(item); // only push the item if is present in one of the original arrays
else zippedItem.push(null); // Otherwise push a null value
});
result.push(zippedItem); // Push the zipped item to the result array
})
return result; // Return the zipped values
}
像這樣使用函式:
const prev = ['A', 'C', 'D', 'E'];
const curr = ['A', 'B', 'D', 'F'];
const zipped = zip([prev, curr]);
這將產生這個結果;
[
[ 'A', 'A' ],
[ null, 'B' ],
[ 'C', null ],
[ 'D', 'D' ],
[ 'E', null ],
[ null, 'F' ]
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505115.html
標籤:javascript 数组
