I have the following object:
var Customers = {
name = 'John',
last = 'Doe'
}
I have imported to my react component, I'm having difficulty looping through object content.
Here is what I have tried
import Customers from './customer';
var customer = Customers.map(function(s){ return s.name });
I'm getting the following error
Uncaught ReferenceError: name is not defined(…)(anonymous
Use the Object. keys() method to get an array of the object's keys. Use the map() method to iterate over the array of keys.
To map through an object's value in React:Use the Object. values() method to get an array of the object's values. Call the map() method on the array of values.
Using the map() Method The map() method is the most commonly used function to iterate over an array of data in JSX. You can attach the map() method to the array and pass a callback function that gets called for each iteration. When rendering the User component, pass a unique value to the key prop.
Also, you can't use map for objects like this. You should write
var customer = Object.keys(Customers).map(function(s){ return Customers[s].name });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With