[英]Adding arrays to an array of arrays
我確信這非常簡單,我真的不想要一個完整的解決方案,但在我學習的過程中指向正確的方向。
我有:
let randomArray = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
目標是讓它看起來像這樣,像項目一樣分組:
[[1,1,1,1],[2,2,2],[10],[20,20],[391],[392],[591]]
我的以下代碼安排好,分組很好。 我將 temp 推送到我的 Group 陣列。 但是當我重置我的“tempArray”以使其為下一個“組”准備好時,它也會從我的組中刪除數據。 因為有關聯? 我假設? 也許?
最后只剩下最后一項了。
我如何阻止它這樣做?
// My SOlution
let randomArray = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
let tempArray = [];
let groupArray = [];
function cleanTheRoom (arr) {
let ascendingArray = arr.sort(function(a,b) {
return a - b;
});
tempArray.push(randomArray[0])
for (let i = 1; i <= randomArray.length; i++) {
if (randomArray[i] === tempArray[0]) {
tempArray.push(randomArray[i])
} else {
groupArray.push(tempArray);
tempArray = []
tempArray.push(randomArray[i])
}
} console.log(groupArray)
}
cleanTheRoom(randomArray);
我拿了你的代碼並更改了一些部分。
Array#sort
就地Array#sort
。 無需分配給新變量。
通過使用索引減一和索引處的值,從零開始迭代並直接查看數據。 如果不相等,則找到一個新組。 在這種情況下,只需將一個空數組分配給groupArray
並將該組推送到結果。
這種方法不是使用相同的對象引用,而是通過將零分配給length
來清空數組,而是采用一個新數組。
將值推到if
語句之外,因為它在then和else部分被加倍。
最后返回帶有組的數組。
function cleanTheRoom(array) { let result = []; let groupArray; array.sort(function(a, b) { return a - b; }); for (let i = 0; i < array.length; i++) { if (array[i - 1] !== array[i]) { groupArray = []; result.push(groupArray); } groupArray.push(array[i]); } return result; } let randomArray = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]; console.log(cleanTheRoom(randomArray));
您的代碼中存在三個主要問題:
您在函數內部使用randomArray
但您應該使用ascendingArray
tempArray.length = 0
-這個說法變異原tempArray
因為你正在推動tempArray
陣列中的groupArray
,改變tempArray
反映在groupArray
為好。
您可以克隆tempArray
並將tempArray
的副本推tempArray
groupArray
groupArray.push([...tempArray]);
或者您可以將新的空數組分配給tempArray
tempArray = [];
當它是循環的最后一次迭代時,您不會將tempArray
的內容推tempArray
groupArray
。 這將導致groupArray
不包含排序數組中的最后一個數字,即591
。 您需要檢查當前迭代是否是循環的最后一次迭代。 如果是,則推送tempArray
中的groupArray
for (let i = 1; i < ascendingArray.length; i++) { ... if (i == ascendingArray.length - 1) { groupArray.push(tempArray); } }
這是您的代碼的簡化版本:
let randomArray = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]; function cleanTheRoom(arr) { let tempArray = []; let groupArray = []; let ascendingArray = arr.sort(function (a, b) { return a - b; }); for (let i = 0; i < ascendingArray.length; i++) { tempArray.push(ascendingArray[i]); if (ascendingArray[i + 1] !== ascendingArray[i]) { groupArray.push(tempArray); tempArray = []; } } console.log(groupArray); } cleanTheRoom(randomArray);
可能有更快的方法,但我快速嘗試了一下:
我為組使用了 Map 而不是字典,因為我們可以巧妙地利用 Map.set 函數。 它返回我們需要的整個 Map 作為reduce 中的返回值。
const randomArray = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]; const isGroup = (acc, number) => Array.isArray(acc.get(number)); const setGroup = (acc, number) => acc.set(number, isGroup(acc, number) ? acc.get(number).concat([number]) : [number]); const unorderedNumberGroups = randomArray.reduce(setGroup, new Map()); const order = [...unorderedNumberGroups.keys()].sort((a, b) => a - b); const orderedNumberGroups = order.reduce((acc, key) => acc.concat([unorderedNumberGroups.get(key)]), []); console.log(orderedNumberGroups);
這里有一個更易於調試的版本,以便您可以嘗試理解上面的版本:
const randomArray = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]; const isGroup = (acc, number) => Array.isArray(acc.get(number)); const unorderedNumberGroups = randomArray.reduce((acc, number) => { if (isGroup(acc, number)) { const mapEntry = acc.get(number); const newEntry = mapEntry.concat([number]); return acc.set(number, newEntry); // we need to return the acc, the Map.set method returns the whole Map } else { return acc.set(number, [number]); // we need to return the acc, the Map.set method returns the whole Map } }, new Map()); // initialize the accumulator we an empty Map const keysAsArray = [...unorderedNumberGroups.keys()]; const order = keysAsArray.sort((a, b) => a - b); const orderedNumberGroups = order.reduce((acc, key) => { const arrayForTheKey = unorderedNumberGroups.get(key); return acc.concat([arrayForTheKey]); // note the breakets! // equivalent code: // acc.push(arrayForTheKey); // return acc; }, []); // initialize the accumulator with an empty Array console.log(orderedNumberGroups);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.