-1

我有这样的数组

var aaa = [
  [value1,value2],[0,0]],
  [[value3,value4],[0,1]],
  [[value5,value6],[1,0]],
  [[value7,value8],[0,2]],
  [[value9,value10],[1,1]],
  [value11,value12],[2,0]]
];

我想根据值,等将此数组拆分为多个[0,1]数组[0,2]

IE

array1 = [[value1,value2],[value3,value4],[value7,value8]];
array2 = [[value5,value6],[value9,value10]];
array3 = [[value11,value12]];

我怎样才能做到这一点 ?

4

2 回答 2

1

用这个:

var aaa = [
[['value1','value2'],[0,1]],
[['value3','value4'],[0,2]],
[['value5','value6'],[1,0]],
[['value7','value8'],[0,3]],
[['value9','value10'],[1,1]],
[['value11','value12'],[2,0]]];

var result = {};

for (var i = 0; i < aaa.length; i += 1) {
    if (!result[aaa[i][1][0]]) {
        result[aaa[i][1][0]] = [];
    }
    result[aaa[i][1][0]].push(aaa[i][0]);
}

之后:

result[0]; //[[value1,value2],[value3,value4],[value7,value8]];
result[1]; //[[value5,value6],[value9,value10]];
result[2]; //[[value5,value6],[value9,value10]];
于 2013-04-15T09:19:46.947 回答
0

假设:

[['value1','value2'],[0,1]]

在此示例中: 0 告诉必须将值存储在哪个数组中。1 告诉它必须存储在该数组的哪个单元格中。

这段代码可以完成工作:

var aaa = [
    [['value1','value2'],[0,1]],
    [['value3','value4'],[0,2]],
    [['value5','value6'],[1,0]],
    [['value7','value8'],[0,3]],
    [['value9','value10'],[1,1]],
    [['value11','value12'],[2,0]]
];

var arr_result = new Array();

for (var k in aaa) {
    if (arr_result[aaa[k][1][0]] == undefined) {
        arr_result[aaa[k][1][0]] = new Array();
    }
    arr_result[aaa[k][1][0]][aaa[k][1][1]] = aaa[k][0];
}

警告:这可能会让空单元格,就像在这个例子中一样,没有 [0,0] 单元格。

于 2013-04-15T09:30:19.390 回答