React.js使用map

时间:2016-12-02 00:11:15

标签: javascript reactjs

我有以下对象:

var Customers = {
 name = 'John',
 last = 'Doe'
}

我已经导入到我的react组件,我在循环访问对象内容时遇到了困难。

这是我试过的

import Customers from './customer';

var customer = Customers.map(function(s){ return s.name });

我收到以下错误

Uncaught ReferenceError: name is not defined(…)(anonymous 

2 个答案:

答案 0 :(得分:9)

此外,您不能使用map作为此类对象。你应该写

var customer = Object.keys(Customers).map(function(s){ return Customers[s].name });

答案 1 :(得分:6)

而不是等于name='John',而不是name : 'John'

如果您要检索名称,可以访问Customers.name之类的变量。如果你想用它做更多的事情让我知道,我非常乐意提供帮助。

相关问题