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

How to sort object keys

I currently have an object (configuration) that gets mapped over in order to display each item on a wrap up screen. However, I need to do a sort on the object before the map but am having trouble accessing the specific value that I need.

const ConfigComponentSummaryContainer = ({ configuration }) => (
    <Grid container spacing={4}>
      {Object.keys(configuration)
        .map(key => {
          const config = configuration[key];
          if (config && config.length && !restrictList.includes(config[0].component.grouping.toLowerCase())) {
            return config
              .map(c => (
                <Grid item xs={6} ref={componentRef}>
                  <ConfigComponentSummary configuration={c} />
                </Grid>
              ));
          }
          return null;
      })}
    </Grid>
  );

This is what the configuration object looks like in the console:
enter image description here

I need to grab a value (seq) inside of component to use for the sort. How can I can access that specific value and sort before the keys are mapped?

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

>Solution :

Use sort. You can use keys and look up the object like you do in map, or use entries where you get both the key and value.

Object.entries(configuration)
  .sort((a, b) => a[1][0].component.seq - b[1][0].component.seq)
  .map(([key, config]) => {}); 
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