gpt4 book ai didi

javascript - 我们如何搜索并找到与某些 `Array number` 匹配的属性名称 `schools` 的 `search text`

转载 作者:行者123 更新时间:2023-11-30 13:53:52 26 4
gpt4 key购买 nike

我们如何在给定的复杂 Json 数组中搜索并找到与某些搜索文本匹配的属性名称 schools数组编号 below: 我试过下面的代码,但它不起作用。

const searchText = "South School";
var myArr = [];
myArr = response.body.schools;

var results = myArr.filter(function(myArr) {
return myArr.schools.indexOf(searchText) > -1;
});

根据上面的搜索文本,我需要从下面的 json 中找到与搜索文本匹配的数组编号。当我观察 chrome 控制台时,我可以看到以下数组范围。

enter image description here

{  
"lastUpdated":"2019-08-27T00:07:58.7222559+00:00",
"alerts":[
{
"id":"ea229500-bc49-4dda",
"schools":[
{
"id":"b04fc2fb-3e62-4b4e",
"name":"North School-Upper",
"region":"North Island",
"station":"125001",
"lastUpdate":"2019-08-27T00:00:11.7304497+00:00",
"percentageFull":0.00,
"winStorageUrl":"https://testurl.com"
}
],
"lastUpdate":"2019-08-25T03:47:26.0493451+00:00",
"title":"North School",
"subTitle":"Forecast results",
"description":"North School",
"status":1,
"statusDescription":"Draft",
"scheduledFor":"2019-08-28T03:30:00+00:00",
"deletedDate":null,
"isInApp":false,
"isEmailNotification":false,
"facebookPost":null,
"twitterPost":"First twitter post of school"
},
{
"id":"a4d78fdf-bcca-4304",
"schools":[
{
"id":"e8b45981-53aa-4061",
"name":"South School",
"region":"Far South School",
"station":"110A",
"lastUpdate":"2019-08-27T00:00:11.7305428+00:00",
"percentageFull":0.00,
"winStorageUrl":"http://testurl
}
],
"lastUpdate":"2019-08-27T00:01:33.4345038+00:00",
"title":"Some new ideas",
"subTitle":"A blueberry sees an orange.",
"description":"Unfortunately, that is wrong; on the contrary, some skillful snakes",
"status":3,
"statusDescription":"Sent",
"imageUrl":null,
"scheduledFor":null,
"deletedDate":null,
"isInApp":true,
"isEmailNotification":true,
"facebookPost":"Facebook post inserted while updating\n",
"twitterPost":"Twitter post inserted while creating\n"
},....
// JSOn data continues here

]

}

enter image description here

最佳答案

根据你的样本数据,你需要过滤3次。

var searchText = "South School";
var schools = [];
var arr = [
{
"lastUpdated":"2019-08-27T00:07:58.7222559+00:00",
"alerts":[
{
"id":"ea229500-bc49-4dda",
"schools":[
{
"id":"b04fc2fb-3e62-4b4e",
"name":"North School-Upper",
"region":"North Island",
"station":"125001",
"lastUpdate":"2019-08-27T00:00:11.7304497+00:00",
"percentageFull":0.00,
"winStorageUrl":"https://testurl.com"
}
],
"lastUpdate":"2019-08-25T03:47:26.0493451+00:00",
"title":"North School",
"subTitle":"Forecast results",
"description":"North School",
"status":1,
"statusDescription":"Draft",
"scheduledFor":"2019-08-28T03:30:00+00:00",
"deletedDate":null,
"isInApp":false,
"isEmailNotification":false,
"facebookPost":null,
"twitterPost":"First twitter post of school"
},
{
"id":"a4d78fdf-bcca-4304",
"schools":[
{
"id":"e8b45981-53aa-4061",
"name":"South School",
"region":"Far South School",
"station":"110A",
"lastUpdate":"2019-08-27T00:00:11.7305428+00:00",
"percentageFull":0.00,
"winStorageUrl":"http://testurl"
}
],
"lastUpdate":"2019-08-27T00:01:33.4345038+00:00",
"title":"Some new ideas",
"subTitle":"A blueberry sees an orange.",
"description":"Unfortunately, that is wrong; on the contrary, some skillful snakes",
"status":3,
"statusDescription":"Sent",
"imageUrl":null,
"scheduledFor":null,
"deletedDate":null,
"isInApp":true,
"isEmailNotification":true,
"facebookPost":"Facebook post inserted while updating\n",
"twitterPost":"Twitter post inserted while creating\n"
}
]

},
{
"lastUpdated":"2019-08-27T00:07:58.7222559+00:00",
"alerts":[
{
"id":"ea229500-bc49-4dda",
"schools":[
{
"id":"b04fc2fb-3e62-4b4e",
"name":"North School-Upper",
"region":"North Island",
"station":"125001",
"lastUpdate":"2019-08-27T00:00:11.7304497+00:00",
"percentageFull":0.00,
"winStorageUrl":"https://testurl.com"
}
],
"lastUpdate":"2019-08-25T03:47:26.0493451+00:00",
"title":"North School",
"subTitle":"Forecast results",
"description":"North School",
"status":1,
"statusDescription":"Draft",
"scheduledFor":"2019-08-28T03:30:00+00:00",
"deletedDate":null,
"isInApp":false,
"isEmailNotification":false,
"facebookPost":null,
"twitterPost":"First twitter post of school"
},
{
"id":"a4d78fdf-bcca-4304",
"schools":[
{
"id":"e8b45981-53aa-4061",
"name":"South School",
"region":"Far South School",
"station":"110A",
"lastUpdate":"2019-08-27T00:00:11.7305428+00:00",
"percentageFull":0.00,
"winStorageUrl":"http://testurl"
}
],
"lastUpdate":"2019-08-27T00:01:33.4345038+00:00",
"title":"Some new ideas",
"subTitle":"A blueberry sees an orange.",
"description":"Unfortunately, that is wrong; on the contrary, some skillful snakes",
"status":3,
"statusDescription":"Sent",
"imageUrl":null,
"scheduledFor":null,
"deletedDate":null,
"isInApp":true,
"isEmailNotification":true,
"facebookPost":"Facebook post inserted while updating\n",
"twitterPost":"Twitter post inserted while creating\n"
}
]

},
{
"lastUpdated":"2019-08-27T00:07:58.7222559+00:00",
"alerts":[
{
"id":"ea229500-bc49-4dda",
"schools":[
{
"id":"b04fc2fb-3e62-4b4e",
"name":"North School-Upper",
"region":"North Island",
"station":"125001",
"lastUpdate":"2019-08-27T00:00:11.7304497+00:00",
"percentageFull":0.00,
"winStorageUrl":"https://testurl.com"
}
],
"lastUpdate":"2019-08-25T03:47:26.0493451+00:00",
"title":"North School",
"subTitle":"Forecast results",
"description":"North School",
"status":1,
"statusDescription":"Draft",
"scheduledFor":"2019-08-28T03:30:00+00:00",
"deletedDate":null,
"isInApp":false,
"isEmailNotification":false,
"facebookPost":null,
"twitterPost":"First twitter post of school"
},
{
"id":"a4d78fdf-bcca-4304",
"schools":[
{
"id":"e8b45981-53aa-4061",
"name":"South School",
"region":"Far north School",
"station":"110A",
"lastUpdate":"2019-08-27T00:00:11.7305428+00:00",
"percentageFull":0.00,
"winStorageUrl":"http://testurl"
}
],
"lastUpdate":"2019-08-27T00:01:33.4345038+00:00",
"title":"Some new ideas",
"subTitle":"A blueberry sees an orange.",
"description":"Unfortunately, that is wrong; on the contrary, some skillful snakes",
"status":3,
"statusDescription":"Sent",
"imageUrl":null,
"scheduledFor":null,
"deletedDate":null,
"isInApp":true,
"isEmailNotification":true,
"facebookPost":"Facebook post inserted while updating\n",
"twitterPost":"Twitter post inserted while creating\n"
}
]

}
];

arr.filter(each => {
each.alerts.filter(alert => {
alert.schools.filter(school => {
if (school.name.indexOf(searchText) > -1) {
schools.push(school.name);
}
});
});
});
console.log(schools);

关于javascript - 我们如何搜索并找到与某些 `Array number` 匹配的属性名称 `schools` 的 `search text`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com