javascript - 如何在 React Native ListView 中过滤和呈现重复出现的嵌套 JSON 数据?

标签 javascript arrays json react-native filter

我是 JSON 和过滤的新手。下面的 data.json 文件和 FolderScreen.jsreact native ListView 中呈现以下章节结构。使用 react-navigation props 向下传递以在 ListView 中呈现相关的嵌套子章节。

这对 Chapter 1 完美无缺,并在 中呈现 Subchapter ABC > ListView

Level 1 > Chapter 1

Subchapter A

Subchapter B

Subchapter C

... 但是一旦 Sub-Subchapters A 被传递到 FolderScreen.js 其所有 Sub-Subchapters A1A2 A3ListView 中未按预期呈现,下面的代码片段......

Level 2 > Subchapter A

Sub-Subchapter A1

Sub-Subchapter A2

Sub-Subchapter 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 属性之前,要么添加一个,要么只添加一个 checkin 。

我最喜欢的技术是使用 (item.content || []).map(...,如果属性为 null 或未定义,则使用空数组。

关于javascript - 如何在 React Native ListView 中过滤和呈现重复出现的嵌套 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030013/

相关文章:

javascript - 简单的 SVG 路径不起作用

javascript - AngularJS:如何判断我的 Controller 是否绑定(bind)到我的 ng-repeat 指令元素?我的数据没有显示

python - 如何获取 python 数组中的所有 "ids"

c# - 如何在没有多次转换的情况下逐字定义小数数组?

java - 调用 REST API 的次数

javascript - 如何停止setInterval

Javascript 函数对象,this 关键字指向错误的对象

javascript - 是什么导致 Squarespace 站点中初始化为 = [] 的数组在第一次引用它之前用对象填充?

javascript - 获取具有定义类型的 JSON 对象长度

javascript - polymer :铁选择器问题