[英]How to filter and render recurring nested JSON data in react native ListView?
I'm new to JSON and filtering.我是 JSON 和过滤的新手。 The data.json file and FolderScreen.js below render the following chapter structure in a
react native
ListView
.下面的data.json文件和FolderScreen.js在React
react native
ListView
中呈现以下章节结构。 With react-navigation
props are passed down to render the related nested subchapters in the ListView
.使用
react-navigation
props 向下传递以呈现ListView
的相关嵌套子章节。
This works flawlessly for Chapter 1
and rendering it's Subchapter A
, B
and C
in the ListView
…这对于
Chapter 1
完美无缺,并在ListView
呈现它的Subchapter A
、 B
和C
......
Level 1 > Chapter 1
级别 1 >第 1 章
Subchapter A
分章A
Subchapter B
B分章
Subchapter C
C分章
… but as soon as Subchapter A
is passed down to the FolderScreen.js all its Sub-Subchapters A1
, A2
and A3
are not rendered as expected in the ListView
with the snippet below… …但是一旦
Subchapter A
传递给FolderScreen.js,它的所有Sub-Subchapters A1
、 A2
和A3
就不会在ListView
按预期呈现,下面的代码片段......
Level 2 > Subchapter A
级别 2 > 分章 A
Sub-Subchapter A1
子章节 A1
Sub-Subchapter A2
子章节 A2
Sub-Subchapter A3
子章节 A3
…am I missing something in the JSON filter? ...我在 JSON 过滤器中遗漏了什么吗?
Or just doing it wrong?还是只是做错了?
{ "id":"chapter-1", "name":"Chapter 1", "type":"folder", "content":[ { "id":"sub-chapter-a", "name":"Subchapter A", "type":"folder", "content":[ { "id":"sub-sub-chapter-a1", "name":"Sub-Subchapter A1", "type":"file" }, { "id":"sub-sub-chapter-a2", "name":"Sub-Subchapter A2", "type":"file" }, { "id":"sub-sub-chapter-a3", "name":"Sub-Subchapter A3", "type":"file" } ] }, { "id":"sub-chapter-b", "name":"Subchapter B", "type":"file" }, { "id":"sub-chapter-c", "name":"Subchapter C", "type":"file" } ] }
renderRow = () => { const entry = this.props.navigation.getParam('chapterID', ''); const listEntry = jsonData.map(all => all.content.filter(parent => parent.id === entry).map(item => item.content.map(i => { return ( <ListEntry id={i.id} name={i.name} type={i.type} navigation={this.props.navigation} key={i.id} /> ); }) ) ); return listEntry; };
This is an easy one.这是个简单的。 Your code expects there to always be a
content
property which is an array (or an object that has a .map()
method, anyway).您的代码期望始终有一个
content
属性,它是一个数组(或具有.map()
方法的对象,无论如何)。
In the innermost level, there is no "content": []
property.在最内层,没有
"content": []
属性。
Either add one, or just add a check in for the content
property before you try to use it.在您尝试使用它之前,添加一个,或者只是为
content
属性添加一个签入。
My favorite technique is to use (item.content || []).map(...
, to use an empty array if the property is null or undefined.我最喜欢的技术是使用
(item.content || []).map(...
,如果属性为空或未定义,则使用空数组。
Your last map returns an array with only one entry.您的最后一张地图返回一个只有一个条目的数组。 You need to access the first array item [0] or modify your code to iterate through results.
您需要访问第一个数组项 [0] 或修改您的代码以遍历结果。
Here is a simplified version of what works:这是有效的简化版本:
const listEntry = jsonData.content
.filter(sub => sub.id === entry)
.map(subsub => subsub.content)[0]
.map(item => {
return <ListEntry id={item.id} name={item.name}/>
}
);
So you should change your code to access the first array member before using the last .map:因此,在使用最后一个 .map 之前,您应该更改代码以访问第一个数组成员:
const listEntry = jsonData.map(all =>
all.content.filter(parent => parent.id === entry).map(item =>
item.content)[0].map(i => {
return (
<ListEntry
id={i.id}
name={i.name}
type={i.type}
navigation={this.props.navigation}
key={i.id}
/>
);
})
)
);
Here is a working Snack for my sample code: https://snack.expo.io/@navardan/nested-contents这是我的示例代码的工作小吃: https : //snack.expo.io/@navardan/nested-contents
Here is also a good article on .map and .filter Simplify your JavaScript – Use .map(), .reduce(), and .filter()这里还有一篇关于 .map 和 .filter 的好文章简化你的 JavaScript – 使用 .map()、.reduce() 和 .filter()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.