Redux:显示单个记录,但json是一个数组
我的react redux应用程序得到一条JSON格式的记录,但该记录是一个数组,因此看起来像这样(注意[]括号): 因此,redux商店将其显示为person->0->{“PersonID”:1,“Name”:“John Smith”,“Gender”:0}。因此,状态显示person对象为空:Redux:显示单个记录,但json是一个数组,json,reactjs,redux,Json,Reactjs,Redux,我的react redux应用程序得到一条JSON格式的记录,但该记录是一个数组,因此看起来像这样(注意[]括号): 因此,redux商店将其显示为person->0->{“PersonID”:1,“Name”:“John Smith”,“Gender”:0}。因此,状态显示person对象为空: Name: this.props.person?this.props.person.Name:'object is empty', My PersonPage.js包含如下详细信息页面: <P
Name: this.props.person?this.props.person.Name:'object is empty',
My PersonPage.js包含如下详细信息页面:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
这是我的原始重复状态:
people: [
[
{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}
]
]
Person
是一个数组
,其中包含有Name键的对象
,因此您需要使用索引
,也可以这样写:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
检查此示例:
var数据={
“人”:[
{
“人格”:1,
“姓名”:“约翰·史密斯”,
“性别”:0
}
]
};
console.log('Name:',data.person[0].Name)
Person
是一个数组
,其中包含有Name键的对象
,因此您需要使用索引
,也可以这样写:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
检查此示例:
var数据={
“人”:[
{
“人格”:1,
“姓名”:“约翰·史密斯”,
“性别”:0
}
]
};
console.log('Name:',data.person[0].Name)代码>我认为您应该为每个人的数据映射人员详细信息
在PersonPage.js上
将其映射如下:
{
this.props.person.map((p)=>{
return (<PersonDetail person={p} />)
})
}
{
this.props.person.map((p)=>{
返回()
})
}
我认为您应该为每个人的数据映射人员详细信息
在PersonPage.js上
将其映射如下:
{
this.props.person.map((p)=>{
return (<PersonDetail person={p} />)
})
}
{
this.props.person.map((p)=>{
返回()
})
}
如果我是你,我会制作一个如下的util函数:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
使用递归,我们检查people是否是数组的一个实例,我们使用返回数组最后一个元素的people.pop()
再次调用该函数。如果我是您,我将创建如下util函数:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
使用递归,我们检查people是否是数组的实例,我们使用people.pop()
再次调用函数,该函数返回数组的最后一个元素。您的person数据中有一个数组。。。您只能在没有0的情况下使用映射访问它
例如:
componentWillReceiveProps = (nextProps) => {
var PersonID = nextProps.person ? nextProps.person.map(item => { return item.PersonID}) : '';
var Name = nextProps.person ? nextProps.person.map(item => { return item.Name}) : '';
this.setState({
PersonID,
Name
});
}
这是考虑到您的个人数据上只有一个数组。您的个人数据上有一个数组。。。您只能在没有0的情况下使用映射访问它
例如:
componentWillReceiveProps = (nextProps) => {
var PersonID = nextProps.person ? nextProps.person.map(item => { return item.PersonID}) : '';
var Name = nextProps.person ? nextProps.person.map(item => { return item.Name}) : '';
this.setState({
PersonID,
Name
});
}
这是考虑到你只有一个人身上的数组。我修正了它!这是给出的两个答案的组合:
在PersonPage.js中,我必须像这样调用PersonDetails对象:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
感谢那些回答的人。这让我发疯 我修好了!这是给出的两个答案的组合:
在PersonPage.js中,我必须像这样调用PersonDetails对象:
<PersonDetail person={this.props.person} />
this.props.person && this.props.person.length ? this.props.person[0].Name : '';
const parsePeople = people => {
if (people instanceof Array) return parsePeople(people.pop())
return people
}
const people = [
[{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}]
]
const person = parsePeople(people)
console.log(person) -> Object
<PersonDetail
person={this.props.person[0]}
/>
感谢那些回答的人。这让我发疯 是的,还有问题。恐怕两个答案都没有帮助。这是我的原始redux状态:是的,仍然存在问题。恐怕两个答案都没有帮助。这是我的原始redux状态:你想要像人一样的东西:[{},{},{}]?或者只是从那里获取对象?我已经有了“人”(这在列表页上有效),但对于单个记录,我只想要“人”。但我可以通过对人的0指数来实现,我也很满意。检查我的答案,我认为写一个util函数来解析“people”是有意义的,你想要类似people的东西:[{},{},{},{}]?或者只是从那里获取对象?我已经有了“人”(这在列表页上有效),但对于单个记录,我只想要“人”。但我可以通过对人的0指数来实现,我也很满意。它看起来不那么干净或合乎逻辑。检查我的答案,我认为写一个util函数来解析“人”看起来合乎逻辑,但给了我一个空白页。看起来合乎逻辑,但给了我一个空白页。对不起,我在控制台中得到一个空白页,其中有以下错误:Uncaught TypeError:无法读取Null的属性“Name”。在这种情况下,您需要在返回时添加验证,因为如果这是错误。。你的第一次渲染是空的。。。要更正此错误,您需要包括返回项.Name?项.Name:“”
而不仅仅是返回项。Name
也应该应用于PersonID。很抱歉,我在控制台中看到一个空白页,上面有此错误:Uncaught TypeError:无法读取null的属性“Name”。在这种情况下,您需要在返回项上添加验证,因为如果这是错误。。你的第一次渲染是空的。。。要更正此错误,您需要包括返回项.Name?项.Name:“”
而不仅仅是返回项。Name
也应应用于PersonID。。