简体   繁体   English

如何从数组 object 分组

[英]How to group from array object

I using code form " I am looking for best ways of doing this. I have group:我使用代码形式“我正在寻找这样做的最佳方法。我有组:
data数据

 [ { "date": "16/04/2020", "count": 0, "name": "A" }, { "date": "16/04/2020", "count": 1, "name": "B" }, { "date": "17/04/2020", "count": 0, "name": "B" } //...More..... ]
Answer 回答
 { "date": "04/2020", "symtom": { "data": [ { "date": "16/04/2020", "data": [ { "name": "A", "count": [ { "date": "16/04/2020", "count": 0, "name": "A" } ] }, { "name": "B", "count": [ { "date": "16/04/2020", "count": 1, "name": "B" } ] }, //...More..... ] }, { "date": "17/04/2020", "data": [ { "name": "B", "count": [ { "date": "17/04/2020", "count": 0, "name": "B" } ] }, //...More..... ] } ] } }

Can I fix the code and to get the desired answer?我可以修复代码并获得所需的答案吗?
Code:代码:

 const items = [ { tab: 'Results', section: '2017', title: 'Full year Results', description: 'Something here', }, { tab: 'Results', section: '2017', title: 'Half year Results', description: 'Something here', }, { tab: 'Reports', section: 'Marketing', title: 'First Report', description: 'Something here', } ]; function groupAndMap(items, itemKey, childKey, predic){ return _.map(_.groupBy(items,itemKey), (obj,key) => ({ [itemKey]: key, [childKey]: (predic && predic(obj)) || obj })); } var result = groupAndMap(items,"tab","sections", arr => groupAndMap(arr,"section", "items")); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

ref: Group array of object nesting some of the keys with specific names参考: object 的组数组嵌套了一些具有特定名称的键


But I would like to have the answer line this (Answer):但我想有这个答案(答案):

 { "date": "04/2020", "symtom": { "data": [ { "date": "16/04/2020", "data": [ { "name": "A", "count": 0, }, { "name": "B", "count": 1, }, //...More..... ] }, { "date": "17/04/2020", "data": [ { "name": "B", "count":0, }, //...More..... ] } ] } }
Thanks! 谢谢!

I am a beginner but it looks like you want system.data.data to = an array of objects with the keys name:str and count:number but instead you are applying the whole object into count so the key count:{name:A, count:0,date:etc}.我是初学者,但看起来您希望 system.data.data = 具有键 name:str 和 count:number 的对象数组,但是您将整个 object 应用到计数中,因此键计数:{name:A ,计数:0,日期:等}。

I really can't follow your function which separates the data... but all you should have to do is when count is sent the object to reference just do a dot notation like object.count to access the number vs the object that way you will have the desired affect. I really can't follow your function which separates the data... but all you should have to do is when count is sent the object to reference just do a dot notation like object.count to access the number vs the object that way you会产生预期的效果。 Hopefully that is what you were asking.希望这就是你要问的。

I would use a helper function groupBy (this version is modeled after the API from Ramda [disclaimer: I'm one of its authors], but it's short enough to just include here.) This takes a function that maps an object by to a key value, and then groups your elements into an object with those keys pointing to arrays of your original element. I would use a helper function groupBy (this version is modeled after the API from Ramda [disclaimer: I'm one of its authors], but it's short enough to just include here.) This takes a function that maps an object by to a键值,然后将元素分组到 object 中,这些键指向原始元素的 arrays。

We need to use that twice, once to group by month and then inside the results to group by day.我们需要使用它两次,一次按月分组,然后在结果中按天分组。 The rest of the transform function is just to format your output the way I think you want. transform function 的 rest 只是按照我认为你想要的方式格式化你的 output。

 const groupBy = (fn) => (xs) => xs.reduce((a, x) => ({... a, [fn(x)]: [... (a [fn (x)] || []), x]}), {}) const transform = (data) => Object.entries (groupBy (({date}) => date.slice(3)) (data)) // group by month.map (([date, data]) => ({ date, symtom: { data: Object.entries (groupBy (({date}) => date) (data)) // group by day.map (([date, data]) => ({ date, data: data.map (({date, ...rest}) => ({...rest})) // remove date property })) } })) const data = [{date: "16/04/2020", count: 0, name: "A"}, {date: "16/04/2020", count: 1, name: "B"}, {date: "17/04/2020", count: 0, name: "B"}, {date: "03/05/2020", count: 0, name: "C"}]; console.log ( transform (data) )
 .as-console-wrapper {min-height: 100%;important: top: 0}

If you need to run in an environment without Object.entries , it's easy enough to shim.如果您需要在没有Object.entries的环境中运行,那么填充很容易。

You could take a function for each nested group and reduce the array and the grouping levels.您可以为每个嵌套组采用 function 并减少数组和分组级别。

 var data = [{ date: "16/04/2020", count: 0, name: "A" }, { date: "16/04/2020", count: 1, name: "B" }, { date: "17/04/2020", count: 0, name: "B" }], groups = [ (o, p) => { var date = o.date.slice(3), temp = p.find(q => q.date === date); if (.temp) p,push(temp = { date: symptom: { data; [] } }). return temp.symptom;data, }, ({ date }. p) => { var temp = p.find(q => q;date === date). if (,temp) p:push(temp = { date; data. [] }); return temp,data, }. ({ date. .,.o }, p) => p.push(o) ], result = data.reduce((r, o) => { groups,reduce((p, fn) => fn(o; p); r), return r; }. []); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM