排序数组(2D数组)需要帮助

时间:2017-12-02 20:55:52

标签: javascript arrays

基本上我想要做的是将像uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])这样的数组输入的值排序到像[[1, 1, 1], [2, 2, 2], [4], [3], [5]]这样的排序的2D数组中。 数组不必是数字/值顺序。下面的代码是我尝试的:

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]);

- &GT; https://repl.it/@John_Nicole/unique

我只有一个参数用于所有输入数组

我有一个unique数组,其中包含1个空行数组。它是一个2D阵列。

然后我会查看[1, 3, 2]的值,并检查unique中的任何数组是否有第一个值作为我的数字(arguments[i][j])。

如果为true,我按下数字arguments[i][j],然后使用splice()删除原始数组中的数字。

如果为false,我会使用此无法识别的值将新数组推入unique

快速概述变量

  • h:这是我要与之比较的unique数组。它可能是[2, 2, 2]
  • i:这是输入中的数组。例如,[1, 3, 2]
  • j:这是号码本身,与i的合作伙伴。例如,arguments[i][j] = [2, 1] - &gt; 2
  • arguments抓取所有,在这种情况下,3个输入数组。
  • Times:这只是意味着0.没有使用过的东西。

输入可以包括像[1, 3, 2], [1, [5]], [2, [4]]

这样的2D数组

这是freeCodeCamp挑战的一部分 - &gt; 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 个答案:

答案 0 :(得分:1)

您可以使用哈希表并检查哈希键是否存在,如果不存在,则将空数组作为哈希值并将其推送到结果集。

  

哈希表是一个对象(这里没有原型),它将值作为键,数组作为值。散列表的末尾将所有值保存在数组中,如果找到新值,则将其插入结果集中。

{
    1: [1, 1, 1],
    2: [2, 2, 2],
    3: [3],
    4: [4],
    5: [5]
}

&#13;
&#13;
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]));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

代码的一些注释(未使用的变量被删除):

&#13;
&#13;
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]));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;