javascript - 如何在 React Native ListView 中过滤和呈现重复的嵌套 JSON 数据?
问题描述
我是 JSON 和过滤的新手。下面的data.json文件和FolderScreen.js以react native
ListView
. With react-navigation
props 被传递下来以渲染ListView
.
这可以完美地用于Chapter 1
和渲染它Subchapter A
,B
并且C
在ListView
......</p>
1级>第1章
A 分章
B 分章
C分章
…但是一旦Subchapter A
被传递到FolderScreen.js中,它的所有Sub-Subchapters A1
,A2
并A3
没有按预期在ListView
下面的代码片段中呈现…</p>
第 2 级 > A 分章
分章 A1
分章 A2
分章 A3
…我在 JSON 过滤器中遗漏了什么吗?
还是只是做错了?
数据.json
{
"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"
}
]
}
文件夹屏幕.js
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;
};
非常感谢您的帮助!
解决方案
这是个简单的。您的代码希望始终有一个content
属性是一个数组(或者是一个具有.map()
方法的对象,无论如何)。
在最内层,没有"content": []
财产。
content
在您尝试使用它之前添加一个,或者只是为该属性添加一个签到。
我最喜欢的技术是使用(item.content || []).map(...
, 如果属性为 null 或未定义,则使用空数组。
推荐阅读
- angular - angular 2 ngPrime 表渲染问题
- php - Symfony OneToOne 关系未在用户实体中获取数据
- reactjs - 为什么在使用 KeyboardAvoidView 时会出现“不变违规”错误?
- r - 用百分比重新排序 ggplot2 中的条形图
- java - HashMap containsKey 对于相同的字符串总是返回 false
- ubuntu - 无法更新 plesk 面板
- r - 在 R 代码中将 NaN 隐藏为零
- javascript - 如何在 JavaScript 中检测“不同的对象类型”?(数组 vs Json vs Date 等...)
- javascript - javascript中bind(this)的复合用法
- php - PHPUnit 没有看到固定装置的变化