按 Javascript 对象键 + 数组分组
Group by Javascript Object keys + array
我正在尝试使用下划线将我的对象数组分组到一个较小的列表中:
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
var groups = _.groupBy(list, function(value){
return value.Region;
});
var grouped = _.map(groups, function(group){
return {
Region: group[0].Region,
Vals: group[0].Vals
}
});
这会有点接近,但它缺少一些 Vals。我想要 'vals' 的数组基于键连接。
var list = [
{ Region: 'A', Vals: [ 7, 'H', 40, 'VH' ] },
{ Region: 'B', Vals: [ 40, 'H', 24, 'VH' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] }
];
list.reduce(function (memo, v) {
if (memo[v.Region]) {
memo[v.Region] = memo[v.Region].concat(v.Vals)
} else {
memo[v.Region] = v.Vals.slice()
}
return memo
}, {})
输出将如下所示:
{
A: [ 7, "H", 40, "VH" ],
B: [40, "H", 24, "VH" ],
C: [ 4, "VH" ]
}
使用带有引用对象的本机 JavaScript Array#reduce
方法来保存索引。
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
// object for refering index
var ref = {};
// iterate over the array
var res = list.reduce(function(arr, v) {
// check property defined if not define and push
// value to array
if (!(v.Region in ref)) {
ref[v.Region] = arr.length;
arr.push({Region: v.Region, Vals: v.Vals});
// if index already defined then push values
} else{
[].push.apply(arr[ref[v.Region]].Vals, v.Vals);
}
// return the array reference
return arr;
// set initial value as empty array
}, []);
console.log(res);
更新: 如果你想生成一个对象,其中 Region
值作为 属性 名称和 Vals
值作为它的值然后做像这样。
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
// iterate over the array
var res = list.reduce(function(obj, v) {
// define the property as an array if not
// already defined
obj[v.Region] = obj[v.Region] || [];
// push all values to array
[].push.apply(obj[v.Region], v.Vals);
// return the object reference
return obj;
// set initial value as an empty object
}, {});
console.log(res);
这将产生您正在寻找的准确输出。
//first build an object with Region properties, and add the Vals to each of those properties
var tempList = {};
for (var i=0; i < list.length; i++) {
if (!(list[i].Region in tempList)) {
tempList[list[i].Region] = [];
}
Array.prototype.push.apply(tempList[list[i].Region],list[i].Vals);
}
//then format this as an array of objects
var groupedList = [];
for (var region in tempList) {
groupedList.push({Region:region, Vals: tempList[region]});
}
list = groupedList;
在上面的代码之后,以下将为真:
list = [
{ Region: 'A', Vals: [ 7, 'H', 40, 'VH' ] },
{ Region: 'B', Vals: [ 40, 'H', 24, 'VH' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] }
];
我正在尝试使用下划线将我的对象数组分组到一个较小的列表中:
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
var groups = _.groupBy(list, function(value){
return value.Region;
});
var grouped = _.map(groups, function(group){
return {
Region: group[0].Region,
Vals: group[0].Vals
}
});
这会有点接近,但它缺少一些 Vals。我想要 'vals' 的数组基于键连接。
var list = [
{ Region: 'A', Vals: [ 7, 'H', 40, 'VH' ] },
{ Region: 'B', Vals: [ 40, 'H', 24, 'VH' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] }
];
list.reduce(function (memo, v) {
if (memo[v.Region]) {
memo[v.Region] = memo[v.Region].concat(v.Vals)
} else {
memo[v.Region] = v.Vals.slice()
}
return memo
}, {})
输出将如下所示:
{
A: [ 7, "H", 40, "VH" ],
B: [40, "H", 24, "VH" ],
C: [ 4, "VH" ]
}
使用带有引用对象的本机 JavaScript Array#reduce
方法来保存索引。
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
// object for refering index
var ref = {};
// iterate over the array
var res = list.reduce(function(arr, v) {
// check property defined if not define and push
// value to array
if (!(v.Region in ref)) {
ref[v.Region] = arr.length;
arr.push({Region: v.Region, Vals: v.Vals});
// if index already defined then push values
} else{
[].push.apply(arr[ref[v.Region]].Vals, v.Vals);
}
// return the array reference
return arr;
// set initial value as empty array
}, []);
console.log(res);
更新: 如果你想生成一个对象,其中 Region
值作为 属性 名称和 Vals
值作为它的值然后做像这样。
var list = [
{ Region: 'A', Vals: [ 7, 'H' ] },
{ Region: 'B', Vals: [ 40, 'H' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] },
{ Region: 'A',Vals: [ 40, 'VH' ] }
];
// iterate over the array
var res = list.reduce(function(obj, v) {
// define the property as an array if not
// already defined
obj[v.Region] = obj[v.Region] || [];
// push all values to array
[].push.apply(obj[v.Region], v.Vals);
// return the object reference
return obj;
// set initial value as an empty object
}, {});
console.log(res);
这将产生您正在寻找的准确输出。
//first build an object with Region properties, and add the Vals to each of those properties
var tempList = {};
for (var i=0; i < list.length; i++) {
if (!(list[i].Region in tempList)) {
tempList[list[i].Region] = [];
}
Array.prototype.push.apply(tempList[list[i].Region],list[i].Vals);
}
//then format this as an array of objects
var groupedList = [];
for (var region in tempList) {
groupedList.push({Region:region, Vals: tempList[region]});
}
list = groupedList;
在上面的代码之后,以下将为真:
list = [
{ Region: 'A', Vals: [ 7, 'H', 40, 'VH' ] },
{ Region: 'B', Vals: [ 40, 'H', 24, 'VH' ] },
{ Region: 'B', Vals: [ 24, 'VH' ] },
{ Region: 'C', Vals: [ 4, 'VH' ] }
];