Can we structure the Javascript object like this using reduce or something else?

Ankit Mahindrakar

I have object something like below which I am getting from API;

const people = [
                  { name: 'Alex joe', age: 28 ,date:'2022-08-22'},
                  { name: 'Alice', age: 21 ,date:'2022-08-18'},
                  { name: 'Anki', age: 25 ,date:'2022-08-22'},
                  { name: 'Max', age: 20 ,date:'2022-08-18'},
                  { name: 'Jane', age: 20 ,date:'2022-08-20'},
                  { name: 'John schinger', age: 17 ,date:'2022-08-22'},
                  { name: 'Alice', age: 21 ,date:'2022-08-20'},
                  { name: 'Max', age: 20 ,date:'2022-08-20'},
                  { name: 'Jane', age: 20 ,date:'2022-08-22'},
               ];

I want it to be sorted wrt date mentioned in the each object like below, so that I can use it in SectionList of React.

const output = [
  {title:'2022-08-18', data:[
                              { name: 'Alice', age: 21 ,date:'2022-08-18'},
                              { name: 'Max', age: 20 ,date:'2022-08-18'}
                            ]
  },
  {title:'2022-08-20', data:[
                              { name: 'Jane', age: 20 ,date:'2022-08-20'},
                              { name: 'Alice', age: 21 ,date:'2022-08-20'},
                              { name: 'Max', age: 20 ,date:'2022-08-20'}        
                            ]
  },
  {title:'2022-08-22', data:[
                                { name: 'Jane', age: 20 ,date:'2022-08-22'},
                                { name: 'Anki', age: 25 ,date:'2022-08-22'},
                                { name: 'Alex joe', age: 28 ,date:'2022-08-22'},
                                { name: 'John schinger', age: 17 ,date:'2022-08-22'},        
                            ]
  }
  ]

I want to use this data in my sectionList which asks for title and data as key, tried to use reduce but failed to get the result... can please someone help me out with it, Thanks..

Ehsan Khakian
people.reduce((acc, item) => {
  const existingDateIndex = acc.findIndex(
    (element) => element.title === item.date
  );

  if (existingDateIndex > -1) {
    acc[existingDateIndex].data.push(item);
  } else {
    acc.push({ title: item.date, data: [item] });
  }
  return acc;
}, []);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can we change the execution context of the browser's JavaScript from Window object to something else?

Using something like ngRepeat in Javascript

Is there someway I can use an array or something else to reduce the Code here?

How can we make Javascript arrays immutable like for Objects we have Object.freeze() method?

Can we use something like ampersand for animation keyframe name?

Can't we do something like if (y = Foo()) > 2: in python?

Javascript - reduce the given object into one data structure

Can we get an object of class using a string in javascript?

Can we write IF statement without else in javascript

Is there something like cata but where you can match inner structure?

Can I call a .java file from a .html file with href or something else without using javascript in-between

Is it possible to search in javascript object with something like LIKE operator by string?

Can we initialize an array if we use it with a structure object? | C#

Is this an object or function or something else?

Can we use object as a key of an object in Javascript?

Converting an object of nested objects / arrays into something array like in javascript

How can I shorten my code with for() or something else in JavaScript?

Using jq how can I replace the name of a key with something else

Can we make database return something else,different from 0 when the field in empty

Can we add properties to immutable object in JavaScript?

calculate the count of object using reduce or filter in javascript

Convert string into an object using reduce javascript

How To Group Object Using reduce Function javascript

changing object value using reduce in javascript

Can we draw a circle that uses Path object? [in arguments, like drawPath()]

Python: can we get the value from the dict or object like the ts?

Can Anyone optimize the code reduce the if else condition using for loop

Can i create a group or something like that in C using #define?

How can I do something like foreach in Stata using R?