Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js iterate through object keys and values using map

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 
like image 877
Deano Avatar asked Dec 02 '16 00:12

Deano


People also ask

How do you iterate object keys in react JS?

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.

How do you map data from an object in react JS?

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.

How do you iterate through a JSON object in Reactjs?

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.


1 Answers

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 });
like image 119
Gayane Avatar answered Nov 06 '22 23:11

Gayane