带有React.js的JavaScript映射函数为我提供索引,但不是索引中的值
因此,我的React文件中有类似的内容,效果很好:带有React.js的JavaScript映射函数为我提供索引,但不是索引中的值,javascript,reactjs,map-function,Javascript,Reactjs,Map Function,因此,我的React文件中有类似的内容,效果很好: {this.props.profile.first_name} 上面的代码将输出其中的实际值,例如:“johndoe” 但是,当我尝试循环检查每个配置文件是否为空时(例如,如果用户从未输入过名字),我无法显示实际内容。我得到未定义的索引或索引。所以下面的代码对我没有帮助 var props = Object.keys(this.props.profile).map(function(key, index, value) {
{this.props.profile.first_name}
上面的代码将输出其中的实际值,例如:“johndoe”
但是,当我尝试循环检查每个配置文件是否为空时(例如,如果用户从未输入过名字),我无法显示实际内容。我得到未定义的索引或索引。所以下面的代码对我没有帮助
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>
);
}
})
var-props=Object.keys(this.props.profile).map(函数(键、索引、值){
console.log(键);####给我索引
console.log(索引);####未定义
log(key[value])####提供数据,但提供标签名,而不是
实际价值
console.log(索引[值])##未定义
如果([键[值]){
返回(
{key}{[value]}
);
}
})
我如何获取其中的实际数据值,以便像这样“{this.props.profile.first_name}键或值再次检查其中的实际内容(即“John Doe”),而不仅仅是索引或未定义的内容?
对象。例如,键
返回带有对象
键的
数组
this.props.profile = {
first_name: 'name'
};
Object.keys(this.props.profile)
返回['first\u name']
,以便在映射中获取值
Object.keys(this.props.profile).map(function (key) {
console.log(this.props.profile[key]);
}, this);
对象。键
返回带有对象
键的
数组,例如
this.props.profile = {
first_name: 'name'
};
Object.keys(this.props.profile)
返回['first\u name']
,以便在映射中获取值
Object.keys(this.props.profile).map(function (key) {
console.log(this.props.profile[key]);
}, this);
谢谢!效果很好!感谢JDFIDLE示例-非常有用谢谢!效果很好!感谢JDFIDLE示例-非常有用