JavaScript Map Function with React.js 给我索引,但不是索引中的值
JavaScript Map Function with React.js gives me index, but not value in the index
所以我的 React 文件中有这样的东西,工作正常:
{this.props.profile.first_name}
上面的代码会在那里输出实际值:例如"John Doe"。
但是,当我尝试遍历每一个以检查某些配置文件是否为空时(例如,如果用户从未输入过名字),我无法获得要显示的实际内容。我得到未定义或索引。所以下面的代码对我没有帮助。
var props = Object.keys(this.props.profile).map(function(key, index, value) {
console.log(key); ### gives me index
console.log(index); ### undefined
console.log(key[value]); ### gives me data, but a label name, not the
actually value
console.log(index[value]); ##undefined
if ([key[value]) {
return (
<li className="list-group-item">
<label>{key}</label>{[value]}
</li>
);
}
})
我如何获取其中的实际数据值,以便像这样“{this.props.profile.first_name} 键或值再次检查其中的实际内容(即 "John Doe"),而不仅仅是索引还是未定义?
Object.keys
returns
带有 Object
键的数组,例如你有
this.props.profile = {
first_name: 'name'
};
Object.keys(this.props.profile)
returns ['first_name']
,为了在地图中获得价值,你应该做
Object.keys(this.props.profile).map(function (key) {
console.log(this.props.profile[key]);
}, this);
所以我的 React 文件中有这样的东西,工作正常:
{this.props.profile.first_name}
上面的代码会在那里输出实际值:例如"John Doe"。
但是,当我尝试遍历每一个以检查某些配置文件是否为空时(例如,如果用户从未输入过名字),我无法获得要显示的实际内容。我得到未定义或索引。所以下面的代码对我没有帮助。
var props = Object.keys(this.props.profile).map(function(key, index, value) {
console.log(key); ### gives me index
console.log(index); ### undefined
console.log(key[value]); ### gives me data, but a label name, not the
actually value
console.log(index[value]); ##undefined
if ([key[value]) {
return (
<li className="list-group-item">
<label>{key}</label>{[value]}
</li>
);
}
})
我如何获取其中的实际数据值,以便像这样“{this.props.profile.first_name} 键或值再次检查其中的实际内容(即 "John Doe"),而不仅仅是索引还是未定义?
Object.keys
returns
带有 Object
键的数组,例如你有
this.props.profile = {
first_name: 'name'
};
Object.keys(this.props.profile)
returns ['first_name']
,为了在地图中获得价值,你应该做
Object.keys(this.props.profile).map(function (key) {
console.log(this.props.profile[key]);
}, this);