Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display unordered list in a tabular style

I have a JSON data that looks like the following:

const data = [{name:"a",version:1}, {name:"a",version:2}, {name:"b",version:3}, {name:"c",version:4}, {name:"c",version:5}]

I need to display the above data in the following format:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Would appreciate any ideas or suggestions on how can I approach this problem. I was thinking of creating a table but would like to know if anyone has a more elegant solution.

>Solution :

A simple way to solve this is using reduce function to build the result that you’re looking for, and add just the value of the version concatenated with text "V" and then push it in. Here’s a working solution:

const groupBy = (objectArray, property) => {
  return objectArray.reduce((acc, obj) => {
    const key = obj[property];
    const curGroup = acc[key] ?? [];

    return { ...acc, [key]: [...curGroup, `V${obj.version}`] };
  }, {});
}

const data = [{name:"a",version:1},{name:"a",version:2},{name:"b",version:3},{name:"c",version:4},{name:"c",version:5}]

console.log(groupBy(data, "name"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading