Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 递归过滤搜索栏的json数据_Javascript_Reactjs_Ecmascript 6_Filter_Uisearchbar - Fatal编程技术网

Javascript 递归过滤搜索栏的json数据

Javascript 递归过滤搜索栏的json数据,javascript,reactjs,ecmascript-6,filter,uisearchbar,Javascript,Reactjs,Ecmascript 6,Filter,Uisearchbar,我正在尝试为react表编写搜索栏过滤器 困难的部分是JSON数据结构,它可能具有未知深度的父子关系,例如: data = [ { input: "row 0 ", output: "output of the first row" subRows: [], }, { input: "row 1", output: "output of t

我正在尝试为react表编写搜索栏过滤器

困难的部分是JSON数据结构,它可能具有未知深度的父子关系,例如:

  data = [
    {
      input: "row 0 ",
      output: "output of the first row"
      subRows: [],
    },
    {
      input: "row 1",
      output: "output of the second row"
      subRows: [
        {
          input: "third row 1.0",
          output: "output of the 3° row"
          subRows: [
            {
              input: "row 1.0.0",
              output: "output of the third row"
              subRows: [],
            },
          ],
        },
        {
          input: "row 1.1",
          output: "output of 2 rows after the third (fifth)"
          subRows: []
        },
      ],
    },
  ];
附加信息:上述JSON的每个对象在MyHTML(jsx)表中都表示为一行,其子窗口是其他行,用户单击即可展开

我想我可以用ES6的过滤功能处理它,但我不知道怎么做

这就是我所期望的结果,如果用户在搜索栏中写下“第三个”

以下是我的代码:

 const updateFilter = (filterValue) => {
   let results = data.filter(function f(row) {
      if (Object.values(row).includes(filterValue.toLowerCase())) {
        return true;
      }
      if (row.subRows && row.subRows.length > 0) {
        return (row.subRows = row.subRows.filter(f));
      }
    });
    console.log(JSON.stringify(results));
  };
 }
下面是代码笔中的代码:

谢谢你的帮助


编辑:结果是我无法展平行,因为表需要树状结构中的数据才能呈现,因此下面编写的展平行的解决方案不可行。

您可以先展平嵌套对象,然后相应地过滤值。我还认为使用
Object.values(…).includes(…)
的过滤机制无法正常工作

以下是展平对象并对其进行过滤的示例:

//递归返回包含元素本身及其子元素的数组
具有子行(条目)的函数{
返回条目。flatMap(条目=>[
条目,…扁平化子行(entry.subRows)
]);
}
//获取仅包含对象的数组
//在字符串属性中包含searchString
函数getAllMatchingItems(搜索字符串,数组){
const isMatching=entry=>Object.values(条目)
.filter(val=>typeof val=='string')
.map(val=>val.toLowerCase())
.some(val=>val.includes(searchString.toLowerCase());
返回子行(数组)。筛选器(isMatching)
}
const filtered=getAllMatchingItems(“第三”,数据);
console.log(过滤);

我们是否也可以清空子行中的subRows属性?否则我们将有重复的过滤值,对吗?是的,如果过滤数组中不需要实际的子窗口,您完全可以这样做
 const updateFilter = (filterValue) => {
   let results = data.filter(function f(row) {
      if (Object.values(row).includes(filterValue.toLowerCase())) {
        return true;
      }
      if (row.subRows && row.subRows.length > 0) {
        return (row.subRows = row.subRows.filter(f));
      }
    });
    console.log(JSON.stringify(results));
  };
 }