简体   繁体   English

如何使用reduce将对象转换为对象数组?

[英]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. json存储在“monitorings”变量中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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