简体   繁体   English

JSON 对象数组。 按名称对对象值进行分组

[英]JSON array of objects. group the objects values on name

I'm trying to get with Javascript JSON object values to be grouped based on the name key word in the array that contains the objects this is a dynamic array so this cannot be hard coded.我正在尝试使用 Javascript JSON object 值根据包含对象的数组中的名称关键字进行分组,这是一个动态数组,因此不能硬编码。

the objects have time values in it对象中有时间值

the time_entries array length changes. time_entries 数组长度发生变化。

the time_entries array can have 1 or more objects in it. time_entries 数组中可以包含 1 个或多个对象。

I want to group the objects by the time: Hour minutes seconds我想按时间对对象进行分组:小时分钟秒

{
    "activities": [
        {
            "name": "player one",
            "time_entries": [
                {
                    "days": 0,
                    "end_time": "2019-09-30 15:19:43",
                    "hours": 01,
                    "minutes": 02,
                    "seconds": 11,
                    "start_time": "2019-09-30 14:17:58"
                },
                {
                    "days": 0,
                    "end_time": "2019-09-25 15:40:11",
                    "hours": 0,
                    "minutes": 20,
                    "seconds": 4,
                    "start_time": "2019-09-25 15:20:15"
                },
                {
                    "days": 0,
                    "end_time": "2019-09-25 16:10:15",
                    "hours": 0,
                    "minutes": 30,
                    "seconds": 4,
                    "start_time": "2019-09-25 15:40:11"
                },

#there can be more objects here

            ]
        },
        {
            "name": "player two",
            "time_entries": [
                {
                    "days": 0,
                    "end_time": "2019-09-30 19:18:51",
                    "hours": 0,
                    "minutes": 0,
                    "seconds": 52,
                    "start_time": "2019-09-30 19:17:58"
                },

#there can be more objects here

            ]
        },
        {
            "name": "player three",
            "time_entries": [
                {
                    "days": 0,
                    "end_time": "2019-09-30 19:19:09",
                    "hours": 0,
                    "minutes": 0,
                    "seconds": 58,
                    "start_time": "2019-09-30 19:18:51"
                },
                {
                    "days": 0,
                    "end_time": "2019-09-30 21:21:09",
                    "hours": 2,
                    "minutes": 1,
                    "seconds": 0,
                    "start_time": "2019-09-30 19:20:09"
                },

#there can be more objects here

            ]
        }
    ]
}

What I want to get is something like this我想得到的是这样的

 [
        {
            "name": "player one",
            "time_entries": [
                {
                    "days": 0,
                    "end_time": "2019-09-25 16:10:15",
                    "hours": 01,
                    "minutes": 50,
                    "seconds": 19,
                    "start_time": "2019-09-30 14:17:58"
                }
            ]
        },
        {
            "name": "player two",
            "time_entries": [
                {
                    "days": 0,
                    "end_time": "2019-09-30 19:18:51",
                    "hours": 0,
                    "minutes": 0,
                    "seconds": 52,
                    "start_time": "2019-09-30 19:17:58"
                }
            ]
        },
        {
            "name": "player three",
            "time_entries": [
                {
                    "days": 0,
                    "end_time":"2019-09-30 21:21:09",
                    "hours": 2,
                    "minutes": 1,
                    "seconds": 58,
                    "start_time": "2019-09-30 19:18:51"
                }
            ]
        }
]

You can do it as follows:你可以这样做:

  • Use map() method to iterate on given activities and transform each activity's time_entries property.使用map()方法迭代给定的活动并转换每个活动的time_entries属性。
  • getTimeEntry() method takes array of time entries and returns earliest start time, latest end time, and total activity time converted to days, hours, minutes, seconds . getTimeEntry()方法获取时间条目数组并返回最早开始时间、最晚结束时间和转换为days, hours, minutes, seconds的总活动时间。

 let data = { "activities": [ { "name": "player one", "time_entries": [ { "days": 0, "end_time": "2019-09-30 15:19:43", "hours": 01, "minutes": 02, "seconds": 11, "start_time": "2019-09-30 14:17:58" }, { "days": 0, "end_time": "2019-09-25 15:40:11", "hours": 0, "minutes": 20, "seconds": 4, "start_time": "2019-09-25 15:20:15" }, { "days": 0, "end_time": "2019-09-25 16:10:15", "hours": 0, "minutes": 30, "seconds": 4, "start_time": "2019-09-25 15:40:11" } ] }, { "name": "player two", "time_entries": [ { "days": 0, "end_time": "2019-09-30 19:18:51", "hours": 0, "minutes": 0, "seconds": 52, "start_time": "2019-09-30 19:17:58" } ] }, { "name": "player three", "time_entries": [ { "days": 0, "end_time": "2019-09-30 19:19:09", "hours": 0, "minutes": 0, "seconds": 58, "start_time": "2019-09-30 19:18:51" }, { "days": 0, "end_time": "2019-09-30 21:21:09", "hours": 2, "minutes": 1, "seconds": 0, "start_time": "2019-09-30 19:20:09" } ] } ] }; let result = {}; result.activities = data.activities.map(item => { return { "name": item.name, "time_entries": [getTimeEntry(item.time_entries)] }; }); function getTimeEntry(timeEntries) { let earliestTime = timeEntries[0].start_time; let latestTime = timeEntries[0].end_time; // Find earliest start time and latest end time. timeEntries.forEach(entry => { if (new Date(entry.start_time) < new Date(earliestTime)) { earliestTime = entry.start_time; } if (new Date(entry.end_time) > new Date(latestTime)) { latestTime = entry.end_time; } }); // Calculate total seconds. let seconds = timeEntries.reduce((totalSeconds, currentEntry) => { let seconds = 0; seconds += currentEntry.seconds; seconds += currentEntry.minutes * 60; seconds += currentEntry.hours * 60 * 60; seconds += currentEntry.days * 24 * 60 * 60; return totalSeconds + seconds; }, 0); // Convert total seconds to days, hours, minutes, and seconds. let days = Math.floor(seconds / (24 * 60 * 60)); seconds = seconds % (24 * 60 * 60); let hours = Math.floor(seconds / (60 * 60)); seconds = seconds % (60 * 60); let minutes = Math.floor(seconds / 60); seconds = seconds % 60; return { "start_time": earliestTime, "end_time": latestTime, "days": days, "hours": hours, "minutes": minutes, "seconds": seconds }; } console.log(result);

暂无
暂无

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

相关问题 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property 如何从对象数组中的值数组创建新的对象数组。 TypeScript? - How to create a new array of objects from an array of values in an array of objects. TypeScript? 针对另一个对象数组过滤对象数组。 删除数组值与另一个对象数组中的值匹配的项 - Filter array of objects against another array of objects. Remove items with array values that matches those inside another array of objects 用javascript对象填充数组。 通过比较两个String值选择的对象 - Populate array with javascript objects. Objects chosen by comparing two String values 我有一组对象。 我想随机 output 个对象,其值的总和等于八 - I have an array of objects. I want to randomly output objects whose sum of values ​is equal to eight 如何迭代对象数组并按键名对属性值进行分组? - How to iterate an array of objects and group the property values by their key name? 按日期分组并在 JSON 对象数组中找到温度的最小值和最大值 - Group by Date and find the minimum and maximum values of temperature in JSON array of objects 将值作为索引数组的对象分组 - Group objects with values as array of index 根据值对对象数组进行分组 - Group array of objects depending on values 对象数组中的 indexOf 问题。 反应应用 - Problem with indexOf in an array of objects. React app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM