0

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.

1
  • 1
    What is interviewScheduled in your code ? I don't see it in your fields. Commented Nov 24, 2022 at 9:51

2 Answers 2

2

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.

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

2 Comments

I am using Node JS Not in Front End Thanks @jsN00b your answer
Not sure I understand the context of the above comment @Tushar. If you are insinuating that operators such as ??= are only usable on Front End, please check this answer to see which Node JS versions support.
2

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)),
    }
  }
});

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.