1

I have an array of objects like below

[
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-07"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-08"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-09"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-10"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-11"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-07"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-08"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-09"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-10"
        ]
    }
},
{
    "medication": {
        "name": "Turbohaler",
        "time": [
            "2018-09-11"
        ]
    }
},
{
    "medication": {
        "name": "Septron",
        "time": [
            "2018-09-07"
        ]
    }
},
{
    "medication": {
        "name": "Septron",
        "time": [
            "2018-09-08"
        ]
    }
},
{
    "medication": {
        "name": "Septron",
        "time": [
            "2018-09-09"
        ]
    }
},
{
    "medication": {
        "name": "Septron",
        "time": [
            "2018-09-10"
        ]
    }
},
{
    "medication": {
        "name": "Septron",
        "time": [
            "2018-09-11"
        ]
    }
}
]

lodash or underscore provided great functions to minimalise the code, but still those libraries didn't help me solve the above problem

I want to achieve something like below

[
  {
    "medication": {
    "name": "Turbohaler",
    "time": [
    "2018-09-07",
    "2018-09-08",
    "2018-09-09",
    "2018-09-10",
    "2018-09-11"
  ]
 }
},
{
 "medication": {
 "name": "Septron",
 "time": [
    "2018-09-07",
    "2018-09-08",
    "2018-09-09",
    "2018-09-10",
    "2018-09-11"
  ]
}

} ]

The above result is formed by grouping an array of objects with same key name (Medication) in our case, and fetching all its values together and merging it into a single object in an array. I have tried different approaches and it failed, If someone could try a bit for above expected output, it will be much appreciated

Thanks in advance

1

2 Answers 2

2

Here is a O(n) solution for the problem you are facing. In addition, use spread syntax on array so that if the time array has multiple items it is handled properly.

var arr = [{
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-07"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-08"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-09"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-10"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-11"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-07"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-08"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-09"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-10"
      ]
    }
  },
  {
    "medication": {
      "name": "Turbohaler",
      "time": [
        "2018-09-11"
      ]
    }
  },
  {
    "medication": {
      "name": "Septron",
      "time": [
        "2018-09-07"
      ]
    }
  },
  {
    "medication": {
      "name": "Septron",
      "time": [
        "2018-09-08"
      ]
    }
  },
  {
    "medication": {
      "name": "Septron",
      "time": [
        "2018-09-09"
      ]
    }
  },
  {
    "medication": {
      "name": "Septron",
      "time": [
        "2018-09-10"
      ]
    }
  },
  {
    "medication": {
      "name": "Septron",
      "time": [
        "2018-09-11"
      ]
    }
  }
];
var tempObj = {};
arr.forEach((item)=>{
  if(!tempObj[item.medication.name]){
    tempObj[item.medication.name] = item;
  } else {
    tempObj[item.medication.name].medication.time.push(...item.medication.time);
  }
});
var res = Object.values(tempObj);
console.log(res);

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

1 Comment

sounds like a perfect solution, Thanks
2

You could take a Map and then collect all values for a given name and render then a new array.

var data = [{ medication: { name: "Turbohaler", time: ["2018-09-07"] } }, { medication: { name: "Turbohaler", time: ["2018-09-08"] } }, { medication: { name: "Turbohaler", time: ["2018-09-09"] } }, { medication: { name: "Turbohaler", time: ["2018-09-10"] } }, { medication: { name: "Turbohaler", time: ["2018-09-11"] } }, { medication: { name: "Turbohaler", time: ["2018-09-07"] } }, { medication: { name: "Turbohaler", time: ["2018-09-08"] } }, { medication: { name: "Turbohaler", time: ["2018-09-09"] } }, { medication: { name: "Turbohaler", time: ["2018-09-10"] } }, { medication: { name: "Turbohaler", time: ["2018-09-11"] } }, { medication: { name: "Septron", time: ["2018-09-07"] } }, { medication: { name: "Septron", time: ["2018-09-08"] } }, { medication: { name: "Septron", time: ["2018-09-09"] } }, { medication: { name: "Septron", time: ["2018-09-10"] } }, { medication: { name: "Septron", time: ["2018-09-11"] } }],
    grouped = Array.from(
        data.reduce((m, { medication: { name, time } }) => m.set(name, (m.get(name) || []).concat(time)), new Map),
        ([name, time]) => ({ medication: { name, time } })
    );
    
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

looks like a simplified approach. Thanks @Nina

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.