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

Loop const elements and render values

I have the following const:

const dictionaryData = {
  "Car1": "Ford",
  "Car2": "Dodge",
  "Car3": "Chevrolet",
  "Car4": "Nissan",
  "Car5": "Toyota",
  "Car6": "Honda",
};

What I want to do is loop through the CarN elements and render a span with the brand value. Something like this:

<span key="Car1">Ford</span>

I tried doing a dictionaryData.map() but this is not working (getting dictionary.map() is not a function error), I think that it’s because is not an array. So my question is: how can I through that const and access to the brand values? Maybe is not possible change that const to an array.

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 :

The Object.entries() method returns an array of a given object’s own
enumerable string-keyed property [key, value] pairs. This is the same
as iterating with a for…in loop, except that a for…in loop
enumerates properties in the prototype chain as well. read more

Object.entries(dictionaryData).map(([key, value]) => {
 return <span key={key}>{value}</span>
})
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