简体   繁体   中英

How can I map json to specific format in TypeScript?

I have the below json

[
   {
      "fullName":"Mariem",
      "startDate":"1917-04-25",
      "endDate":"1917-04-26",
      "endHour":"1330",
      "motif":"maladie"
   },
   {
      "fullName":"Mariem",
      "startDate":"1917-04-25",
      "endDate":"1917-04-26",
      "endHour":"1800",
      "motif":"renuion"
   },
   {
      "fullName":"Mariem",
      "startDate":"1917-05-25",
      "endDate":"1917-05-25",
      "endHour":"1600",
      "motif":"renuion"
   },
   {
      "fullName":"Jack",
      "startDate":"0017-01-25",
      "endDate":"0017-01-25",
      "endHour":"1030",
      "motif":null
   }
]

Who can i map it like the below json, grouping the objects by fullName, startDate and endDate, and add an array of objects contients the endDate and motif.

[
   {
      "fullName":"Mariem ",
      "startDate":"1917-04-25",
      "endDate":"1917-04-26",
      "data":[
         {
            "endHour":"1330",
            "motif":"maladie"
         },
         {
            "endHour":"1800",
            "motif":"renuion"
         }
      ]
   },
   {
      "fullName":"Mariem ",
      "startDate":"1917-05-25",
      "endHour":"1917-05-25",
      "data":[
         {
            "endHour":"1600",
            "motif":"renuion"
         }
      ]
   },
   {
      "fullName":"Jack",
      "startDate":"0017-01-25",
      "endDate":"0017-01-25",
      "data":[
         {
            "endHour":"1030",
            "motif":null
         }
      ]
   }
]

If I understood your question correctly you are looking for something like this:

interface SomeObj {
  fullName: string,
  startDate: string,
  endDate: string,
  endHour: string,
  motif: string
}

interface Time {
  endHour:string,
  motif:string
}

interface MappedObj {
  fullName: string,
  startDate: string,
  endHour: string,
  data: Time[]
}


function mapToFormat(o: SomeObj[]): MappedObj[] {
  return o.map(item => {
    return {
      fullName: item.fullName,
      startDate: item.startDate,
      endHour: item.endHour,
      data: [
        {
          endHour: item.endHour,
          motif: item.motif
        }
      ]
    }
  })
}

You have errors in both your sample input and output(swapping endHour and endDate ).

Here is a quick solution:

const map = new Map();

elements.forEach(item => {
    const key = `${item.fullName}+${item.startDate}+${item.endDate}`;

    if (!map.has(key)) {
       map.set(key, {
         fullName: item.fullName,
         startDate: item.startDate,
         endDate: item.endDate,
         data: []
       });
    }

    map.get(key).data.push({
      endHour: item.endHour,
      motif: item.motif
    });
});

const result = Array.from(map.values());

With input of:

const elements = [
   {
      "fullName":"Mariem",
      "startDate":"1917-04-25",      
      "endDate":"1917-04-26",
      "endHour":"1330",
      "motif":"maladie"
   },
   {
      "fullName":"Mariem",
      "startDate":"1917-04-25",
      "endDate":"1917-04-26",
      "endHour":"1800",
      "motif":"renuion"
   },
   {
      "fullName":"Mariem",
      "startDate":"1917-05-25",
      "endDate":"1917-05-25",
      "endHour":"1600",
      "motif":"renuion"
   },
   {
      "fullName":"Jack",
      "startDate":"0017-01-25",
      "endDate":"0017-01-25",
      "endHour":"1030",
      "motif":null
   }
];

The result variable will have the value of:

[
   {
      "fullName":"Mariem",
      "startDate":"1917-04-25",
      "endDate":"1917-04-26",
      "data":[
         {
            "endHour":"1330",
            "motif":"maladie"
         },
         {
            "endHour":"1800",
            "motif":"renuion"
         }
      ]
   },
   {
      "fullName":"Mariem",
      "startDate":"1917-05-25",
      "endDate":"1917-05-25",
      "data":[
         {
            "endHour":"1600",
            "motif":"renuion"
         }
      ]
   },
   {
      "fullName":"Jack",
      "startDate":"0017-01-25",
      "endDate":"0017-01-25",
      "data":[
         {
            "endHour":"1030",
            "motif":null
         }
      ]
   }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM