0

Please help to combine object keys values from json arrays by key.

    const input=[{
   "Date":"04/10/2019",
   "Time":"15:35",
   "Title":"Flight -\u00a0Group C",
   "Description":"Arrival 19:40"
},
{
   "Date":"04/10/2019",
   "Time":"Evening",
   "Title":"Welcome drinks",
   "Description":"In the bar at\u00a0"
},
{
   "Date":"05/10/2019",
   "Time":"Morning",
   "Title":"Breakfast",
   "Description":"At leisure"
},
{
   "Date":"05/10/2019",
   "Time":"10:00",
   "Title":"Something Else",
   "Description":"At leisure"
}];

console.log(
  Object.values(input.reduce((a, { Date }) => {
    if (!a[Date]) a[Date] = { Date, activities: [] };
    a[Date].activities.push();
    return a;
  }, {}))
);

I need a combined object keys values in javascript in the following way:

[
   {
      "date":"04/10/2019",
      "activities":[
         {
            "Time":"15:35",
            "Title":"Flight -\u00a0Group C",
            "Description":"Arrival 19:40"
         },
         {
            "Time":"Evening",
            "Title":"Welcome drinks",
            "Description":"In the bar at\u00a0"
         }
      ]
   },
   {
      "date":"05/10/2019",
      "activities":[
         {
            "Time":"Morning",
            "Title":"Breakfast",
            "Description":"At leisure"
         }
      ]
   }
]

I am trying to get the result with my code but 'activities' are null... I would highly appreciate your help.

Thank you in advance

2
  • 2
    Technical but important note: by definition, JSON is pure string data. You can't "do" anything with it until you JSON.parse() it, the result of which is no longer JSON but is normal JS code (arrays, objects, etc). So if you're asking about grouping/sorting/etc, you're not asking about JSON. You're just trying to reorganise data inside plain JS data structures. Commented Feb 19, 2020 at 21:21
  • Does this answer your question? Group array of object nesting some of the keys with specific names Commented Feb 19, 2020 at 21:22

1 Answer 1

2

If you are using parameter destructuring you could use ... rest param to get other properties after you take out the date.

const input=[{"Date":"04/10/2019","Time":"15:35","Title":"Flight - Group C","Description":"Arrival 19:40"},{"Date":"04/10/2019","Time":"Evening","Title":"Welcome drinks","Description":"In the bar at "},{"Date":"05/10/2019","Time":"Morning","Title":"Breakfast","Description":"At leisure"},{"Date":"05/10/2019","Time":"10:00","Title":"Something Else","Description":"At leisure"}]

const object = input.reduce((r, { Date: date, ...rest}) => {
  if(!r[date]) r[date] = {date, activities: [rest]}
  else r[date].activities.push(rest)
  return r
}, {})

const result = Object.values(object)
console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.