使用两个属性对对象进行分组

编程入门 行业动态 更新时间:2024-10-27 12:24:20
本文介绍了使用两个属性对对象进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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/ 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>

更多推荐

使用两个属性对对象进行分组

本文发布于:2023-11-30 05:52:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!