[英]How to filter normalized nested JSON data with no array?
after use normalizr
library I have the following normalized JSON object result in the Redux state of my application: 使用normalizr
库后,我的应用程序的Redux状态具有以下标准化的JSON对象结果:
{
sports: {
byId: {
1: {
id: 1,
name: 'Soccer',
slug: 'soccer'
},
2: {
id: 2,
name: 'Basketball',
slug: 'basketball'
},
3: {
id: 3,
name: 'American Football',
slug: 'american-football'
}
},
allIds: [
'1',
'2',
'3'
]
},
competitions: {
byId: {
'1': {
id: 1,
name: 'Competition 1',
short_name: 'Comp 1',
slug: 'comp-1',
sport: 1,
status: {
is_live: false
}
},
'2': {
id: 2,
name: 'Competition 2',
short_name: 'Comp 2',
slug: 'comp-2',
sport: 1,
status: {
is_live: true
}
},
'3': {
id: 3,
name: 'National Basketball League',
short_name: 'NBA',
slug: 'national-basketball-league',
sport_slug: 'basketball',
sport: 3,
status: {
is_live: true
}
}
},
allIds: [
'1',
'2',
'3'
]
}
What I want achieve: I want a list of competitions
filtered/categorized by sports
. 我想要实现的目标:我想要一个按sports
筛选/分类的competitions
列表。
How can I do that? 我怎样才能做到这一点?
Also I want to be able to group competitions
by it's status.is_live
. 我也希望能够通过status.is_live
对competitions
进行status.is_live
。
So how can I get an list of competitions
breakdown by sport
that status.is_live
equals true and competitions
status.is_live
equals false? 那么,怎样才能让我的列表competitions
被击穿sport
是status.is_live
等于真实competitions
status.is_live
等于假的?
Any help is appreciated! 任何帮助表示赞赏! Thanks 谢谢
If you don't want to use lodash , you can write a .groupBy
function pretty easily ( example ). 如果您不想使用lodash ,可以很容易地编写一个.groupBy
函数( 例如 )。 You'll need to loop over the output object and reassign its children values with a for instead of using .mapValues
. 您需要遍历输出对象,并使用for而不是.mapValues
来重新分配其子级值。
I used lodash in the example, just to point out the logic. 我在示例中使用了lodash,只是为了指出逻辑。
Note: I'd remove the grouping in the data in the original response, and let the client do this on its own - it's easier to work on an unsorted array and filter/map that instead of working against values from an object that has redundant keys (since they represent groups by Id) 注意:我将删除原始响应中的数据分组,然后让客户端自行执行操作-在未排序的数组和过滤器/映射上进行操作比在具有冗余对象的值上进行处理更容易键(因为它们按ID表示组)
let data = { sports: { byId: { 1: { id: 1, name: 'Soccer', slug: 'soccer' }, 2: { id: 2, name: 'Basketball', slug: 'basketball' }, 3: { id: 3, name: 'American Football', slug: 'american-football' } }, allIds: [ '1', '2', '3' ] }, competitions: { byId: { '1': { id: 1, name: 'Competition 1', short_name: 'Comp 1', slug: 'comp-1', sport: 1, status: { is_live: false } }, '2': { id: 2, name: 'Competition 2', short_name: 'Comp 2', slug: 'comp-2', sport: 1, status: { is_live: true } }, '3': { id: 3, name: 'National Basketball League', short_name: 'NBA', slug: 'national-basketball-league', sport_slug: 'basketball', sport: 3, status: { is_live: true } } }, allIds: [ '1', '2', '3' ] } } let competitions = Object.values(data.competitions.byId) // _.chain(arr) keeps returning `lodash` objects // so I don't have to call it separately for every action let filteredCompetitions = _.chain(competitions) .groupBy(i => i.status.is_live) .mapValues(i => _.groupBy(i, 'sport')) .value() // returns the final value console.log(filteredCompetitions)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.