Tushar
Tushar

Reputation: 192

Sum of nested array data using Java Script

I try to added data in to dayResult key using date-wise like in shortlisted [] add all array data in to shortlisted [ ] also sum of slotAvailable my JSON format is below mention kindly help me for to added data.

This is my Input JSON data.

        {
            "date": "2022-08-01",
            "dayResult": [
                {
                    "date": "2022-08-01T18:29:59.999Z",
                    "day": 1,
                    "shortlisted": [
                        "62e76c83b61203589ed06682"
                    ],
                    "slotAvailable": 0
                    
                },
                 {
                    "date": "2022-08-01T18:29:59.999Z",
                    "day": 1,
                    "shortlisted": [
                        "62e76c83b61203589ed06682",
                        "62e7644f4c8f8d7b2a2d661c"
                    ],
                    "slotAvailable": 2
                    
                },
                 {
                    "date": "2022-08-01T18:29:59.999Z",
                    "day": 1,
                    "shortlisted": [],
                    "slotAvailable": 1
                    
                }
            ]
        },
      {
            "date": "2022-08-02",
            "dayResult": [
                {
                    "date": "2022-08-02T18:29:59.999Z",
                    "day": 2,
                    "shortlisted": [
                        "62e76c83b61203589ed06687",
                      "62e7644f4c8f8d7b2a2d661a"
                    ],
                    "slotAvailable": 0
                    
                },
                 {
                    "date": "2022-08-02T18:29:59.999Z",
                    "day": 2,
                    "shortlisted": [
                        "62e76c83b61203589ed06682",
                        "62e7644f4c8f8d7b2a2d661c"
                    ],
                    "slotAvailable": 1
                    
                },
                 {
                    "date": "2022-08-02T18:29:59.999Z",
                    "day": 2,
                    "shortlisted": [],
                    "slotAvailable": 6
                    
                }
            ]
        }
    ] 

This is my wanted out put

[
  {
    "date": "2022-08-01",
    "dayResult": [
      {
        "date": "2022-08-01T18:29:59.999Z",
        "day": 1,
        "shortlisted": [
          "62e76c83b61203589ed06682",
          "62e7644f4c8f8d7b2a2d661c"
        ],
        "slotAvailable": 3
      }
    ]
  },
  {
    "date": "2022-08-02",
    "dayResult": [
      {
        "date": "2022-08-02T18:29:59.999Z",
        "day": 2,
        "shortlisted": [
          "62e76c83b61203589ed06687",
          "62e7644f4c8f8d7b2a2d661a",
          "62e76c83b61203589ed06682",
          "62e7644f4c8f8d7b2a2d661c"
        ],
        "slotAvailable": 7
      }
    ]
  }
]

So for that I try this kind of code but its not works as accepted.

let dateArr = {};
for (let i = 0; i < groupArrays.length; i++) 
{
      const item = groupArrays[i];
      item.dayResult.map((data) =>{
      dateArr.date =  data.date;
      dateArr.day = data.day;
      dateArr.interviewScheduled = [...data.interviewScheduled]
  })
}

Please help me for this kind of out put using javascript.

Upvotes: 0

Views: 71

Answers (2)

AbsoluteZero
AbsoluteZero

Reputation: 401

You can use Array.prototype.map to go through every entry of your data. Then to merge N objects into one you can use Array.prototype.reduce and to remove duplicates from an array you can use [...new Set(Array)];

Here's an example:

const mappedData = data.map((v) => {
    const { date, dayResult } = v;
  
    const mergedDayResults = dayResult.reduce((prev, curr) => ({
        ...prev,
      shortlisted: [
        ...prev.shortlisted,
        ...curr.shortlisted,
      ],
      slotAvailable: prev.slotAvailable + curr.slotAvailable,
    }), dayResult[0])
  
  return {
    date,
    dayResults: {
        ...mergedDayResults,
      shortlisted: Array.from(new Set(mergedDayResults.shortlisted)),
    }
  }
});

Upvotes: 2

jsN00b
jsN00b

Reputation: 3691

Presented below is one possible way to achieve the desired objective.

Code Snippet

// method to transform array to desired structure
const myTransform = arr => (arr.map(  // map each array elt
  ({date: myDt, dayResult: dr}) => ({
  date: myDt,
  dayResult: dr.reduce(  // iterate over inner "dayResult" array
    (acc, {   // destructure & rename date, day, shortlisted, slotAvailable props
      date: myDt2, day: myDy, shortlisted: sh, slotAvailable: sa
    }) => {
      acc ??= {};   // nullish coalesce assignment for accumulator "acc" to empty object
      acc["date"] ??= myDt2;    // assign date, if not already present
      acc["day"] ??= myDy;      // assign day, if not already present
      acc["shortlisted"] ??= [];    // set-up shortlisted as empty array, if not already
      sh.forEach(sItm => {      // add each shortlisted item if it is not already present
        if (!(acc["shortlisted"].includes(sItm))) {
          acc["shortlisted"].push(sItm);
        }
      });
      acc["slotAvailable"] ??= 0;   // set-up slotAvailable as zero, if not already present
      acc["slotAvailable"] += sa;   // increment slotAvailable based on current elt
      return acc;   // always return the accumulator "acc"
    },
    {}      // initialize the "acc" as an empty object
  )})    // implicit return
));


const rawJsonData = [{
  "date": "2022-08-01",
  "dayResult": [{
      "date": "2022-08-01T18:29:59.999Z",
      "day": 1,
      "shortlisted": [
        "62e76c83b61203589ed06682"
      ],
      "slotAvailable": 0

    },
    {
      "date": "2022-08-01T18:29:59.999Z",
      "day": 1,
      "shortlisted": [
        "62e76c83b61203589ed06682",
        "62e7644f4c8f8d7b2a2d661c"
      ],
      "slotAvailable": 2

    },
    {
      "date": "2022-08-01T18:29:59.999Z",
      "day": 1,
      "shortlisted": [],
      "slotAvailable": 1

    }
  ]
}, {
  "date": "2022-08-02",
  "dayResult": [{
      "date": "2022-08-02T18:29:59.999Z",
      "day": 2,
      "shortlisted": [
        "62e76c83b61203589ed06687",
        "62e7644f4c8f8d7b2a2d661a"
      ],
      "slotAvailable": 0

    },
    {
      "date": "2022-08-02T18:29:59.999Z",
      "day": 2,
      "shortlisted": [
        "62e76c83b61203589ed06682",
        "62e7644f4c8f8d7b2a2d661c"
      ],
      "slotAvailable": 1

    },
    {
      "date": "2022-08-02T18:29:59.999Z",
      "day": 2,
      "shortlisted": [],
      "slotAvailable": 6

    }
  ]
}];

console.log('Transformed data:\n', myTransform(rawJsonData));
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Inline comments added to the snippet above.

Upvotes: 2

Related Questions