Deano
Deano

Reputation: 12190

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 

Upvotes: 7

Views: 13622

Answers (2)

Gayane
Gayane

Reputation: 567

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 });

Upvotes: 13

Arturo Lopez
Arturo Lopez

Reputation: 61

Instead of equals name='John' it's suppose to be name : 'John'.

If you are trying to retrieve the name you could access the variable like Customers.name. If you are trying to do something more with it let me know and I am more than happy to help.

Upvotes: 6

Related Questions