gpt4 book ai didi

javascript - ReactJS 循环对象不返回值

转载 作者:行者123 更新时间:2023-11-28 06:00:10 25 4
gpt4 key购买 nike

我有:

let contactData = {};
if (this.props.enquiry.contact) {
contactData.name = this.props.enquiry.contact.firstName + ' ' + this.props.enquiry.contact.lastName;
}

然后:

Object.keys(this.props.data).map((line, idx) => {
console.log(line)
});

但输出是键(名称)而不是值。如何获取 name 参数的值?

最佳答案

像这样

Object.keys(this.props.data).map((line, idx) => {
console.log(this.props.data[line]); // get value from Object by key
});

Object.keys() method returns an array of a given object's own enumerable properties

示例

var obj = { a: 1, b: 2 };

// Object.keys(obj) returns ['a', 'b'];
var res = Object.keys(obj).map((key) => {
// in order to get value from Object by key we can use bracket notation
return obj[key];
});

console.log(res);

关于javascript - ReactJS 循环对象不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37345798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com