排序数组(二维数组)需要帮助
Basiclly我想要做的是将数组输入的值排序为像uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])这样的排序二维数组,如[[1, 1, 1], [2, 2, 2], [4], [3], [5]]。 阵列不一定是编号/值顺序。下面的代码是我的尝试:排序数组(二维数组)需要帮助
function uniteUnique(arr) { let times = 0;
var unique = [[]];
for (var i = 0; i < Array.prototype.slice.call(arguments).length; i++) {
times = 0;
for (var j = 0; j < arguments[i].length; j++) {
for (var h = 0; h < unique.length; h++) {
var pushArray = []
if(unique[h][0] === arguments[i][j]) {
unique[h].push(arguments[i][j])
arguments[i].splice(j)
}
else {
unique.push([arguments[i][j]])
}
}
}
}
return unique
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
- >https://repl.it/@John_Nicole/unique
我只有一个参数对所有的输入数组
我有一个unique阵列1的空白数组。它是一个二维数组。
然后我通过,例如,[1, 3, 2]的值,并检查unique中是否有第一个值作为我的编号(arguments[i][j])。
如果为true,我推动数字arguments[i][j],然后删除原始数组中的数字splice()。
如果为false,我将新阵列插入到unique中,并带有这个无法识别的值。
变量
h的快速概述:这是unique数组,我要与比较。它可能是[2, 2, 2]。这是从输入数组。例如,[1, 3, 2]。j:这个号码本身,它的伙伴i。例如,arguments[i][j]=[2, 1]- >2arguments抓住所有,在这种情况下,3输入数组。Times:这只是意味着0.没有用过这么多。
输入可能包括在自己的二维数组一样[1, 3, 2], [1, [5]], [2, [4]]
这是一个freeCodeCamp挑战的一部分 - >https://www.freecodecamp.org/challenges/sorted-union
我的问题是,为什么我的输出:
[ [], [ 1, 1 ],
[ 5, 5 ],
[ 5 ],
[ undefined, undefined ],
[ 2, 2 ],
[ 2 ],
[ 2 ],
[ 2 ],
[ 2 ],
[ undefined, undefined ],
[ undefined, undefined ],
[ undefined, undefined ],
[ undefined, undefined ] ]
? 当需要的输出是:通缉输出是[1, 1, 1], [2, 2, 2], [4], [3], [5]
我做错了什么?
我得到了多个2的数组,例如([[ 2 ],[ 2 ],[ 2 ],[ 2 ]]),即使我想将所有2放入一个数组中?
回答:
你可以使用一个哈希表,并检查哈希键是否存在,如果没有,然后拿一个空数组作为哈希值,并将它推到结果集。
哈希表是一个对象(这里没有原型),它将值作为键和数组作为值。散列表的末尾保存数组中的所有值,如果找到新值,则将其插入结果集中。
{1: [1, 1, 1],
2: [2, 2, 2],
3: [3],
4: [4],
5: [5]
}
function uniteUnique() { var result = [],
hash = Object.create(null);
Array.prototype.forEach.call(arguments, function (a) {
a.forEach(function (b) {
if (!(b in hash)) {
hash[b] = [];
result.push(hash[b]);
}
hash[b].push(b);
});
});
return result;
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }一些注释代码(未使用的变量被删除):
function uniteUnique() { var unique = [], // move all declarations to top
i, j, h,
pushed;
// array like objects have a length property
for (i = 0; i < arguments.length; i++) {
for (j = 0; j < arguments[i].length; j++) {
pushed = false;
for (h = 0; h < unique.length; h++) {
if (unique[h][0] === arguments[i][j]) {
// take the element, do not use splice, because with splicing
// the array becomes shorter and the index is updated in the
// next loop and is pointing to the element with the wrong index,
// because you get the element after next
// it is better not to mutate a variable, if it works without
// in this case, you iterate and visit each element only once
unique[h].push(arguments[i][j]);
pushed = true; // a value is found
break; // exit this loop
} // prevent more looping
}
if (!pushed) { // if not found
unique.push([arguments[i][j]]); // push the value
}
}
}
return unique;
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }以上是 排序数组(二维数组)需要帮助 的全部内容, 来源链接: utcz.com/qa/258296.html

