[英]Sorting an array of objects into arrays by unique key
I'm pretty rookie at this stuff so I'm expecting that I'm simply missing something extremely obvious but here's the question...我在这方面很菜鸟,所以我期待我只是错过了一些非常明显的东西,但问题是......
I am getting an unknown amount of objects back from a JSON call, I need to match all object keys and put them in their own array.我从 JSON 调用中获取了未知数量的对象,我需要匹配所有对象键并将它们放入自己的数组中。 The problem is that I don't know how many objects are in each array, to show my problem this is a very simplified version of the data I am receiving.
问题是我不知道每个数组中有多少个对象,为了显示我的问题,这是我收到的数据的一个非常简化的版本。
resultOfCall = [
{
'x': 'Lorem',
'y': 'ipsum',
'z': 'dolor'
},
{
'x': 'sit',
'y': 'amet',
'z': 'asdf',
'a': 'qwerty'
},
{
'x': 'consectetur',
'y': 'adipiscing',
'z': 'elit'
}
]
I want to take this data and create an array for every unique key so the above data would look like this...我想获取这些数据并为每个唯一键创建一个数组,以便上述数据看起来像这样......
var a = ['qwerty'];
var x = ['lorem','sit','consectetur'];
var y = ['ipsum', 'amet', 'adipiscing'];
var z = ['dolor', 'asdf', 'elit'];
Now the glaring problem with my above data is that I still need to know that x is called 'x'.现在我上面数据的明显问题是我仍然需要知道 x 被称为“x”。
A few things to note about the data I'm receiving, I don't know how many objects I'm getting back, and I don't know how long any of those objects are.关于我收到的数据的一些注意事项,我不知道我要返回多少个对象,也不知道这些对象中的任何一个有多久。 I just need to match all the keys that I get back.
我只需要匹配我取回的所有密钥。
You can use additional object
for storing your arrays and Array.prototype.forEach
function only, see example您可以使用其他
object
来存储您的数组和Array.prototype.forEach
仅函数,请参阅示例
var resultOfCall = [ { 'x': 'Lorem', 'y': 'ipsum', 'z': 'dolor' }, { 'x': 'sit', 'y': 'amet', 'z': 'asdf', 'a': 'qwerty' }, { 'x': 'consectetur', 'y': 'adipiscing', 'z': 'elit' } ]; var obj = {}; resultOfCall.forEach(function(e) { Object.keys(e).forEach(function(k) { if (obj[k]) { obj[k].push(e[k]); } else { obj[k] = [e[k]]; } }); }); document.write('<pre>' + JSON.stringify(obj,0,2) + '</pre>');
Store the vars as key/value pairs in an object using reduce
:使用
reduce
将变量作为键/值对存储在对象中:
var obj = resultOfCall.reduce(function (p, c) {
Object.keys(c).forEach(function (el) {
p[el] = p[el] || [];
p[el].push(c[el]);
});
return p;
}, {});
OUTPUT输出
{
"x": [
"Lorem",
"sit",
"consectetur"
],
"y": [
"ipsum",
"amet",
"adipiscing"
],
"z": [
"dolor",
"asdf",
"elit"
],
"a": [
"qwerty"
]
}
A version which takes the keys and makes global variables with the array.一个接受键并使用数组创建全局变量的版本。
var resultOfCall = [{ 'x': 'Lorem', 'y': 'ipsum', 'z': 'dolor' }, { 'x': 'sit', 'y': 'amet', 'z': 'asdf', 'a': 'qwerty' }, { 'x': 'consectetur', 'y': 'adipiscing', 'z': 'elit' }]; void function (data) { var r = window; data.forEach(function (a) { Object.keys(a).forEach(function (k) { r[k] = r[k] || []; r[k].push(a[k]); }); }); }(resultOfCall); document.write('<pre>a: ' + JSON.stringify(a, 0, 4) + '</pre>'); document.write('<pre>x: ' + JSON.stringify(x, 0, 4) + '</pre>'); document.write('<pre>y: ' + JSON.stringify(y, 0, 4) + '</pre>'); document.write('<pre>z: ' + JSON.stringify(z, 0, 4) + '</pre>');
Alternative a solution which returns an object with the grouped keys.另一种解决方案,它返回一个带有分组键的对象。
var resultOfCall = [{ 'x': 'Lorem', 'y': 'ipsum', 'z': 'dolor' }, { 'x': 'sit', 'y': 'amet', 'z': 'asdf', 'a': 'qwerty' }, { 'x': 'consectetur', 'y': 'adipiscing', 'z': 'elit' }], grouped = function (data) { var r = {}; data.forEach(function (a) { Object.keys(a).forEach(function (k) { r[k] = r[k] || []; r[k].push(a[k]); }); }); return r; }(resultOfCall); document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.