I've an array of objects with the following format
我有一个具有以下格式的对象数组
[{'list': 'one', 'item': 1},
{'list': 'one', 'item': 2},
{'list': 'one', 'item': 3},
{'list': 'two', 'item': 1},
{'list': 'two', 'item': 2}]
And I want to transform it like this
我想像这样改造它
[{'one': [1, 2, 3]},
{'two': [1, 2]}]
How can i do it using Array.map function? It's the best alternative?
我怎么能用Array.map函数呢?这是最好的选择吗?
3 个解决方案
#1
1
To your specific question:
针对您的具体问题:
// Let x hold your array of objects.
res={}; // Create an empty object that will hold the answer
x.forEach (function (e) { // Use this function to iterate over each item in the list
res[e.list] = res[e.list] || []; // inspired by the Nina Scholz answer below
res[e.list].push(e.item); // Append the result to the array
});
#2
10
You may use Array.prototype.reduce
for your task. It allows a return value in the callback function for the next call.
您可以使用Array.prototype.reduce来完成任务。它允许回调函数中的返回值用于下一次调用。
var data = [
{ 'list': 'one', 'item': 1 },
{ 'list': 'one', 'item': 2 },
{ 'list': 'one', 'item': 3 },
{ 'list': 'two', 'item': 1 },
{ 'list': 'two', 'item': 2 }
],
flat = data.reduce(function (r, a) {
r[a.list] = r[a.list] || [];
r[a.list].push(a.item);
return r;
}, {});
document.write('<pre>' + JSON.stringify(flat, 0, 4) + '</pre>');
#3
1
What you're looking for is a group-by method. This question has a nice answer: https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
您正在寻找的是一种分组方法。这个问题有一个很好的答案:https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
The code:
代码:
function groupBy( array , f )
{
var groups = {};
array.forEach( function( o )
{
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group )
{
return groups[group];
})
}
var result = groupBy(list, function(item)
{
return [item.lastname, item.age];
});
#1
1
To your specific question:
针对您的具体问题:
// Let x hold your array of objects.
res={}; // Create an empty object that will hold the answer
x.forEach (function (e) { // Use this function to iterate over each item in the list
res[e.list] = res[e.list] || []; // inspired by the Nina Scholz answer below
res[e.list].push(e.item); // Append the result to the array
});
#2
10
You may use Array.prototype.reduce
for your task. It allows a return value in the callback function for the next call.
您可以使用Array.prototype.reduce来完成任务。它允许回调函数中的返回值用于下一次调用。
var data = [
{ 'list': 'one', 'item': 1 },
{ 'list': 'one', 'item': 2 },
{ 'list': 'one', 'item': 3 },
{ 'list': 'two', 'item': 1 },
{ 'list': 'two', 'item': 2 }
],
flat = data.reduce(function (r, a) {
r[a.list] = r[a.list] || [];
r[a.list].push(a.item);
return r;
}, {});
document.write('<pre>' + JSON.stringify(flat, 0, 4) + '</pre>');
#3
1
What you're looking for is a group-by method. This question has a nice answer: https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
您正在寻找的是一种分组方法。这个问题有一个很好的答案:https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
The code:
代码:
function groupBy( array , f )
{
var groups = {};
array.forEach( function( o )
{
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group )
{
return groups[group];
})
}
var result = groupBy(list, function(item)
{
return [item.lastname, item.age];
});