简体   繁体   中英

How to convert object into array of object using reduce?

I want to convert object into array of object that fits my needs. I prefer using the most simple solution and smaller amount of code to write. The json is stored inside "monitorings" variable.

monitorings = [
{
    "id": 1,
    "survey_id": 1,
    "region_id": 9101,
    "month_id": 1,
    "target": 22,
    "progress": 22,
    "survey": {
      "name": "HPG",
      "category": "SHP"
    },
  },
  {
    "id": 2,
    "survey_id": 1,
    "region_id": 9102,
    "month_id": 1,
    "target": 10,
    "progress": 10,
    "survey": {
      "name": "SHPED",
      "category": "SHPED"
    },
  },
}
]

My brain can only think until this code

Object.entries(
  monitorings.reduce((monitorings, monitoring) => {
    const { name } = monitoring.survey

    monitorings[name] = monitorings[name]
                      ? [...monitorings[name], monitoring]
                      : [monitoring]

    return monitorings
  }, {})
)

actual output

[
  "survey.name", [{grouped object}],
  "survey.name", [{grouped object}],
]  

expected output

[
  "survey.category", [
    "survey.name", [{grouped object}],
      "survey.name", [{grouped object}],
  ]
 ,
 "survey.category", [
   "survey.name", [{grouped object}],
   "survey.name", [{grouped object}],
 ],
]

Thanks for your help

- Edit -

grouped object's format has the same format as the original object like below

[
  {
    "id": 2,
    "survey_id": 1,
    "region_id": 9102,
    "month_id": 1,
    "target": 10,
    "progress": 10,
    "survey": {
      "name": "SHPED",
      "category": "SHPED"
    },
  },

  {same format as above},
  {same format as above},
  ...
],

i found the answer here and modify it.

Object.entries(monitorings.reduce((map, obj) => {
  !map[obj.survey["category"]] 
    ? map[obj.survey["category"]] = {}  
    : [].concat(obj.survey["name"]).forEach(subEl => {
      !map[obj.survey["category"]][subEl]
        ? map[obj.survey["category"]][subEl] = []
        : map[obj.survey["category"]][subEl].push(obj);
  })

  return map;
  }, {})
)

explanation

//return convert object into array of object
Object.entries(

//return new form of object
monitorings.reduce((map, obj) => {

  //if empty
  !map[obj.survey["category"]] 

    //create new empty object of survey["category"]
    ? map[obj.survey["category"]] = {}

    //else combine all of returned object of survey["name"] into empty array of object  
    : [].concat(obj.survey["name"])

        //iterate over obj.survey["name"]
        .forEach(subEl => {

          //if that object is empty
          !map[obj.survey["category"]][subEl]

            //create empty array of survey["category"][subEl]
            ? map[obj.survey["category"]][subEl] = []

          //else push every element of filtered original JSON into array of survey["category"][subEl]
          : map[obj.survey["category"]][subEl].push(obj);
  })

  //return grouped object
  return map;
  }, {})
)

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