[英]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.