[英]How to search for elements inside JSON array of objects
I am trying to create a search filter for accordion data which has to search in the title as well as the contents inside.我正在尝试为手风琴数据创建一个搜索过滤器,它必须在标题和里面的内容中搜索。 It must search for the
name
key.它必须搜索
name
键。
I have implemented a function for the same but with that approach, I am not able to search for name
inside the children
key.我已经为相同的功能实现了一个功能,但是使用这种方法,我无法在
children
键中搜索name
。
let filteredTiles = this.tileData.filter((value) => { //tileData is the JSON array
return value.name.toLowerCase().indexOf(searchValue) != -1 ? value : null;
});
How is it possible to search within the children from the JSON data?如何从 JSON 数据中搜索子项?
Demo at Stackblitz Stackblitz 演示
My JSON data is我的 JSON 数据是
[
{
"name": "First",
"image": "https://img.freepik.com/free-vector/football-2022-tournament-cup-background_206725-604.jpg?size=626&ext=jpg",
"children": [
{
"name": "Firstone",
"image": "https://img.freepik.com/free-vector/hand-painted-watercolor-abstract-watercolor-background_23-2149005675.jpg?size=626&ext=jpg",
"url": "http://www.google.com"
},
{
"name": "Firsttwo",
"image": "https://img.freepik.com/free-vector/hand-painted-watercolor-abstract-watercolor-background_23-2149005675.jpg?size=626&ext=jpg",
"url": "http://www.google.com"
},
{
"name": "Firstthree",
"image": "https://img.freepik.com/free-vector/hand-painted-watercolor-abstract-watercolor-background_23-2149005675.jpg?size=626&ext=jpg",
"url": "http://www.google.com"
}
]
},
{
"name": "Second",
"image": "https://media.istockphoto.com/id/517188688/photo/mountain-landscape.jpg?s=612x612&w=0&k=20&c=A63koPKaCyIwQWOTFBRWXj_PwCrR4cEoOw2S9Q7yVl8=",
"children": [
{
"name": "Secondone",
"image": "https://img.freepik.com/free-vector/hand-painted-watercolor-abstract-watercolor-background_23-2149005675.jpg?size=626&ext=jpg",
"url": "http://www.google.com"
},
{
"name": "Secondtwo",
"image": "https://media.istockphoto.com/id/517188688/photo/mountain-landscape.jpg?s=612x612&w=0&k=20&c=A63koPKaCyIwQWOTFBRWXj_PwCrR4cEoOw2S9Q7yVl8=",
"url": "http://www.google.com"
},
{
"name": "Secondthree",
"image": "https://img.freepik.com/free-vector/hand-painted-watercolor-abstract-watercolor-background_23-2149005675.jpg?size=626&ext=jpg",
"url": "http://www.google.com"
}
]
},
]
This works similarly on how you filter the object in an array.这与您如何过滤数组中的对象类似。
In the predicate, you have to filter the nested object in the array.在谓词中,您必须过滤数组中的嵌套对象。 If the filtering of a nested array return index greater than -1, means that there are nested object(s) that fulfill the filter criteria.
如果嵌套数组的过滤返回索引大于-1,则表示存在满足过滤条件的嵌套对象。
Besides, you can work with Array.some(predicate)
which returns boolean value as alternative of Array.findIndex(predicate) > -1
.此外,您可以使用返回布尔值的
Array.some(predicate)
作为Array.findIndex(predicate) > -1
的替代方法。
this.tileData.filter((value) => {
return
value.children.some(
(c) => c.name.toLowerCase().indexOf(searchValue) > -1
)
;
});
If you want to filter whether the root object or nested children fulfill the filter criteria, you should use the ||
如果要过滤根对象或嵌套子对象是否满足过滤条件,则应使用
||
(or) operator. (或)运营商。
let filteredTiles = this.tileData.filter((value) => {
return (
value.name.toLowerCase().indexOf(searchValue) > -1 ||
value.children.some(
(c) => c.name.toLowerCase().indexOf(searchValue) > -1
)
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.