我有以下从API提取中返回的对象
{
id: 1,
name: "Leanne Graham",
zipcode: "92998-3874",
username: "Bret",
email: "Sincere@april.biz",
..... (only the ones above matter to me)
}
我只希望提取对象的某些部分(例如邮政编码)。我尝试了以下操作:this.props.users.zipcode
JSON.parse
JSON.stringify
,但是在登录控制台时,它们全部都返回undefined
。你介意帮我吗?
答案 0 :(得分:0)
我从您的评论中得知您正在使用Redux。
API提取的结果应该是一个动作。在关联的化简器中,您需要检索成功请求触发的操作的有效负载。
正如其他人之前指出的那样,如果您提供了代码示例,那么答案将更容易,而不仅仅是在黑暗中开火
答案 1 :(得分:0)
仅考虑数据,以下可能的解决方案将帮助您获取键的值。
import React, {Component} from 'react';
export default class Test extends Component {
render(){
let data = [{"id":1,"name":"Leanne Graham","username":"Bret","email":"Sincere@april.biz","address":{"street":"Kulas Light","suite":"Apt. 556","city":"Gwenborough","zipcode":"92998-3874","geo":{"lat":"-37.3159","lng":"81.1496"}},"phone":"1-770-736-8031 x56442","website":"hildegard.org","company":{"name":"Romaguera-Crona","catchPhrase":"Multi-layered client-server neural-net","bs":"harness real-time e-markets"}}]
data.map((d, i) => {
console.log("values", d.name, d.email, d.address.zipcode )
});
return(
<div>
</div>
)
}
}
或
import React, {Component} from 'react';
export default class Test extends Component {
render(){
if(this.props.users && this.props.users.map((d, i) => {
console.log("values", d.name, d.email, d.address.zipcode )
});
return(
<div>
</div>
)
}
}