我有一个api休息,返回以下对象:
对象
[ {attributes:{State:Las vegas,Period:1991 ,人物:6000,孩子:3000 } }, {属性:{州:拉斯维加斯,期间:2000,人物:5000,孩子:1000 } } ]我需要一个类似的小组:
我以前的退货
{People_2000: [ {州:拉斯维加斯,人民:5000 } ],People_1991: [ {州:拉斯维加斯,人民:6000 } ],Child_1991 : [ {州:拉斯维加斯,孩子:3000 } ],Child_2000: [ {州:拉斯维加斯,孩子:1000 } ] }解释
用于在表中添加您的值。
我正在使用版本的lodash:4.17.4
我也在版本中使用vue.js:2.3.3
我的分组不符合预期, jsfiddle/9fh0b3q7/4/使用 lodash #map ,定义一个属性数组,希望将属性数组与来自某个期间的特定前缀。使用 lodash #reduce 一种组合所有分组属性的方法。用于分组的 lodash #group 每个映射的属性,最后 lodash#assign 将所有属性及其各自的组包含在一个对象中。
var attributes = _.map(数据,'属性'); var properties = ['State','People','Child']; var result = _.reduce(properties,(acc,property)=> _(attributes) .groupBy(v => property +'_'+ v.Period) .assign(acc) .value(), {} );var data = [{attributes:{State:Las vegas,Period:1991,People:6000}},{attributes:{State:Las vegas ,Period:2000,People:5000}}]; var attributes = _.map(data,'attributes'); var properties = ['State','People','Child']; var result = _.reduce(properties,(acc,property)=> _(attributes).groupBy(v => property +'_'+ v.Period).assign(acc).value(),{}); console.log(JSON.stringify(result,0,4));
.as-console-wrapper {min-height:100%; top:0;}
< script src = cdnjs.cloudflare/ajax/libs/lodash.js/4.17.4/lodash.min.js>< /脚本>
i have an api in rest that return the following object :
The object
[ { "attributes": { "State" : "Las vegas", "Period": 1991, "People": 6000, "Child" : 3000 } }, { "attributes": { "State" : "Las vegas", "Period": 2000, "People": 5000, "Child" : 1000 } } ]I need a group at the like with this :
My previous return
{ "People_2000": [ { "State" : "Las vegas", "People": 5000 } ], "People_1991": [ { "State" : "Las vegas", "People": 6000 } ], "Child_1991": [ { "State" : "Las vegas", "Child": 3000 } ], "Child_2000": [ { "State" : "Las vegas", "Child": 1000 } ] }Explanation
For add in table yours values.
I am using the version of lodash : 4.17.4
Also am using vue.js at the version: 2.3.3
my grouping is not as expected, jsfiddle/9fh0b3q7/4/
解决方案We can get all attributes first using lodash#map, define an array of properties that you want your array of attributes to be grouped with a certain prefix from a certain Period. Use lodash#reduce as a way to combine all the grouped properties. lodash#group for grouping each of the mapped attributes, and finally lodash#assign to contain all the properties and their respective groups into one object.
var attributes = _.map(data, 'attributes'); var properties = ['State', 'People', 'Child']; var result = _.reduce(properties, (acc, property) => _(attributes) .groupBy(v => property + '_' + v.Period) .assign(acc) .value(), {} ); var data = [ { "attributes": { "State" : "Las vegas", "Period": 1991, "People": 6000 } }, { "attributes": { "State" : "Las vegas", "Period": 2000, "People": 5000 } } ]; var attributes = _.map(data, 'attributes'); var properties = ['State', 'People', 'Child']; var result = _.reduce(properties, (acc, property) => _(attributes) .groupBy(v => property + '_' + v.Period) .assign(acc) .value(), {} ); console.log(JSON.stringify(result, 0, 4));.as-console-wrapper{min-height:100%;top:0;}
<script src="cdnjs.cloudflare/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>