简体   繁体   English

从键中提取通用值并创建多维数组

[英]Extract common values from a key and make a multidimensional array

I have an array of objects like this: 我有一个这样的对象array

[
{"k1":"v1","k2":27172},
{"k1":"v1","k2":15177},
{"k1":"v1","k2":19411},
{"k1":"v2","k2":30163},
{"k1":"v2","k2":23949},
{"k1":"v2","k2":23829},
{"k1":"v2","k2":27821}
]

What's the easiest way to extract the common values from k1 and create a multidimensional array like the below? 从k1提取公共值并创建如下所示的多维数组的最简单方法是什么?

[
["v1", [27172, 15177, 19411]],
["v2", [30163, 23949, 23829, 27821]]
];

Here is a solution with some loops. 这是一些循环的解决方案。

I created a jsbin here: http://jsbin.com/cucixi/edit?html,js,console 我在这里创建了一个jsbin: http ://jsbin.com/cucixi/edit?html,js,console

var list = [
            {"k1":"v1","k2":27172},
            {"k1":"v1","k2":15177},
            {"k1":"v1","k2":19411},
            {"k1":"v2","k2":30163},
            {"k1":"v2","k2":23949},
            {"k1":"v2","k2":23829},
            {"k1":"v2","k2":27821}
         ];
var data = {};
list.forEach(function(item){
  if (item.k1 in data){
     data[item.k1].push(item.k2);
  } else {
     data[item.k1] = [item.k2];
  }
});

var output = [];
for (var key in data){
  output.push([key, data[key]]);
}

console.log(output);

Output: 输出:

[
   ["v1", [27172, 27172, 15177, 15177, 19411, 19411]], 
   ["v2", [30163, 30163, 23949, 23949, 23829, 23829, 27821, 27821]]
]

Using reduce function of array 使用数组的reduce函数

var test = [
    { "k1": "v1", "k2": 27172 },
    { "k1": "v1", "k2": 15177 },
    { "k1": "v1", "k2": 19411 },
    { "k1": "v2", "k2": 30163 },
    { "k1": "v2", "k2": 23949 },
    { "k1": "v2", "k2": 23829 },
    { "k1": "v2", "k2": 27821 }
]

var out = test.reduce(function(res, obj) {
    res[obj.k1] = res[obj.k1] || [];
    res[obj.k1].push(obj.k2)
    return res;
}, {});

var result = [];
for (var key in out) {
    result.push([key, out[key]])
};

console.log(JSON.stringify(result, null, 2))

will give 会给

[
 ["v1", [27172, 15177, 19411]],
 ["v2", [30163, 23949, 23829, 27821]]
];

jsfiddle: https://jsfiddle.net/aLnc9oh1/ jsfiddle: https ://jsfiddle.net/aLnc9oh1/

The solution: 解决方案:

function solution(A)
{
    // make an object with an array of values
    var temp = {};
    for (var i=0, ii=A.length; i<ii; i++)
    {
        var prop = A[i]['k1'];
        if (temp[prop])
            temp[prop].push(A[i]['k2']);
        else
            temp[prop] = [A[i]['k2']];
    }

    // reformat that into the 2D array like desired
    var retArr = [];
    for (var key in temp)
        retArr.push([key, temp[key]]);

    // return it!
    return retArr;
}

For the given data returns: 对于给定的数据返回:

[
    ["v1", [27172, 15177, 19411]],
    ["v2", [30163, 23949, 23829, 27821]]
]

You might want to use a combination of Array.prototype.map() and Object.keys() 您可能要结合使用Array.prototype.map()Object.keys()

var obj = data.reduce((acc, x) => {
  acc[x.k1] = (acc[x.k1] || []).concat(x.k2));
  return acc;
}, {});

var res = Object.keys(obj).map(x => [x, obj[x]]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM