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

Reactjs: How to get values from array of objects

I am a beginner in rectjs. In my project, there is a list of objects stored in "groupedItems", and using sort() to order them and stored them into a variable "numAscending".

enter image description here

Now its structure is in the above picture. but I want to change them like in the below picture.

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

enter image description here

Here is the code which I tried.

 groupedItems.map((items)=>{
                numAscending = [...items].sort((a, b) => a.subsolution_name - b.subsolution_name);
                temp.push({numAscending})
            })
            console.log("temp", temp)


            // result = temp.map((option, key) => {
            //     console.log("eeeeeeee", option)
            //     option.map((item)=>{
            //         console.log("iteee", item)
            //     })
            // });

            // console.log('result', result);
            

I tried to fix it but failed (lines are started with //). Please give me a suggestion to solve this problem.

>Solution :

You probably could flatten the groupedItems array’s element values, which appear to be arrays, and then sort the overall result array.

Example:

const result = groupedItems
  .flat(Number.POSITIVE_INFINITY)
  .sort((a, b) => a.subsolution_name - b.subsolution_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