0

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},
  ...
],
6
  • What should the actual output be? Like, without the {object filtered by ...}? Commented Mar 27, 2019 at 6:57
  • Is monitorings an array of JSON? Commented Mar 27, 2019 at 6:57
  • @JackBashford i've edited the question, you can see above Commented Mar 27, 2019 at 7:01
  • @Madhavan.V Yes, i forgot to put array notation, i already edit the question Commented Mar 27, 2019 at 7:02
  • So not filtered, but sort of grouped by category and name? `['SHP', ['HPG', [{...}, {...}]],['SHPED', ['SHPED', [{...},{...}]]]'. Commented Mar 27, 2019 at 7:16

1 Answer 1

0

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;
  }, {})
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.