Transform array of objects into object that contains an array of objects (Complex)

convert object to array of objects javascript
convert object to array js
convert object to array typescript
convert array object to array javascript
convert object to array javascript es6
convert object to array angular 6
how to convert object into arrays
convert object to array jquery

I am working with React Native Calendars and attempting to structure my data for the agenda component.

The expected data structure is (an Object)

 '2012-05-22': [{text: 'item 1 - any js object'}],
 '2012-05-23': [{text: 'item 2 - any js object'}],
 '2012-05-24': [],
 '2012-05-25': [{text: 'item 3 - any js object'},{text: 'any js object'}],

When fetching events from my api, the events are returned in the following format (an array of Objects)`

 let eventsFetchedFromApi = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Third Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Second Event"},

I have managed to use the following code to turn my arrray of objects into an object (unfortunately it is not very readable, atleast not to me).

let transformedEvents = Object.assign({ startDateTime, comments }) => ({ [startDateTime.substring(0, 10)]: [{comments: [comments]}] })));  

You can use reduce to group an array of objects into a single object indexed by the date string. You can't use map if the input and output items aren't one-to-one, as in your case.

const eventsFetchedFromApi=[{startDateTime:"2018-02-01T11:10:43",comments:"First Event"},{startDateTime:"2018-03-01T11:12:43",comments:"Second Event"},{startDateTime:"2018-02-01T11:18:43",comments:"Third Event"}];
const output = eventsFetchedFromApi.reduce((a, { startDateTime, comments }) => {
  const prop = startDateTime.slice(0, 10);
  if (!a[prop]) a[prop] = [];
  a[prop].push({ comments });
  return a;
}, {});

Javascript, Converting an Object to an Array of Objects., A while back I wrote an article that got some traction about converting an Array of Objects to an Object. Today we are going to do the opposite, convert an object  How to Convert an Array Into an Object Using JavaScript. This method is used to copy values from one or more source objects to a new object.

Try the following:

var arr1 = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Second Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Third Event"},
 var result = {};
 var date = obj.startDateTime.slice(0,10); 
    result[date] = [];
   result[date].push({"comments" : obj.comments});

Data Structures: Objects and Arrays :: Eloquent JavaScript, Objects allow us to group values—including other objects—to build more and 10 p.m., Jacques finds himself transforming into a small furry rodent with a bushy tail. Both string and array values contain, in addition to the length property, To find out what properties an object has, you can use the Object.keys function. What we do in the snippet above is use reduce, which returns accumulator (obj), to which we append each item in the array using that items id. This will convert our array of objects into an object

var resultObject = {};
let eventsFetchedFromApi = [{
        startDateTime: "2018-02-01T11:10:43",
        comments: "First Event"
        startDateTime: "2018-03-01T11:12:43",
        comments: "Second Event"
        startDateTime: "2018-02-01T11:18:43",
        comments: "Third Event"

eventsFetchedFromApi.forEach((val) => {
    let date = val.startDateTime.split("T")[0];
    resultObject[date] = resultObject[date] || {};
    resultObject[date].startDateTime = val.startDateTime;
    resultObject[date].comments = resultObject[date].comments || []
        comments: val.comments
const finalResultArray = Object.values(resultObject);

How to convert an Object {} to an Array [] of key-value pairs in , Objects are more complex and each object may contain any combination of these primitive data-types as well as reference data-types, while the array is a single  If you recall from my previous blog post on reduce, you can use reduce to transform an array into whatever you want, a single value, a collection of values, a collection of collections, etc. Therefore, you can use reduce to transform an array into an object. Let's check it out.

Would this reducer do what you want?

const dt2Date = dateStr => dateStr.split("T")[0];
const createOrPush = (obj, key, value) => obj[key] 
  ? obj[key].push(value) && obj[key] 
  : [].concat(value);
const reducer = (redo, {startDateTime, comments}) => 
  Object.assign( redo, { [dt2Date(startDateTime)]: 
    createOrPush(redo, dt2Date(startDateTime), {comments: comments}) });

const array2Convert = [
  {startDateTime:"2018-02-01T11:10:43", comments: "First Event"},
  {startDateTime:"2018-03-01T11:12:43", comments: "Third Event"},
  {startDateTime:"2018-02-01T11:18:43", comments: "Second Event"},

console.log(array2Convert.reduce( reducer, {} ));

DataWeave - Transforming an Array to an Object, How to transform Arrays into Objects in DataWeave signature like (':array', ':​function') => ':object' (meaning the function has one input of type :array , another input of Below is a more complex example of the above pattern. Here is a need to convert an array into an object. To do so we are going to use a few of the most preferred methods. First here is a method to know. Object.assign() method This method copies the values of all enumerable own properties from source objects(one or more) to a target object. Syntax: Object.assign(target, sources) Parameters:

How to simplify your codebase with map(), reduce(), and filter() in , When you read about Array.reduce and how cool it is, the first and sometimes the Create an object that contains the frequency of the specified key Sometimes you want to print your array of objects with selected keys as a  We have an arr array that contains objects as individual elements. In essence, we have an array of objects. In essence, we have an array of objects. We'll use the object.assign() method to convert this array of objects into an object of objects.

Converting Object to an Array, it's official now! We have 3 variations to convert an Object to an Array in JavaScript. to an Array. Array has an array of methods (sorry, bad pun ). Ancient times. Long long ago, in a far far galaxy, looping over Objects was not so easy. In this blog post, we are going to see the ways we can convert Object to/from Array in javascript/Typescript/Angular applications. First, we will see how Array of the object can be created in typescript. An array of Objects Declaration and Initialization. For example, array contains model data like below.

Array methods, The arrays are objects, so we can try to use delete : "complex" and "language" arr . splice ( 2 , 0 , "complex" , "language" ) ; alert ( arr ) ; // "I", "study", But if an array-like object has a special Symbol. To transform the array:. I've written a method that takes an object whose keys are arrays and converts it to an array of objects. I feel like the code is fairly complex, after waiting a week or so and revisiting it, I don't think anyone would be able to really understand what is going on, especially if they aren't used to this style of coding.

  • Your desired output has the second event in "2018-02-01" but in eventsFetchedFromApi the second event has startDateTime of "2018-03-01T...?
  • My mistake! Silly mix up.