gpt4 book ai didi

javascript - 过滤嵌套的 JSON javascript

转载 作者:行者123 更新时间:2023-11-29 18:10:50 25 4
gpt4 key购买 nike

我正在创建一个采用如下 JSON 的 API:

 "hightlights":[
{
"title":"Fun",
"url":"fun/index.html",
"queries":
[
"music",
"artists",
"events",
"internet"
]
},
{
"title":"Internet",
"url":"internet/index.html",
"queries":
[
"javascript",
"web",
"internet",
]
}
]

我需要用用户给定的词过滤 JSON,然后返回另一个 JSON,其中只有包含“查询”中的词的对象。

 If word === 'music', receive:
{
"title":"Fun",
"url":"fun/index.html",
"queries":[
"music",
"artists",
"events",
"internet"
]
}

If word === 'internet', receive:
{
{
"title":"Fun",
"url":"fun/index.html",
"queries":[
"music",
"artists",
"events",
"internet"
]
},
{
"title":"Internet",
"url":"internet/index.html",
"queries":[
"javascript",
"web",
"internet",
]
}

我的问题是如何嵌套这个值?如果有人能给我一些例子...我将不胜感激...

最佳答案

尝试以下操作:

function getResult(filterBy, objList) {
return objList.hightlights.filter(function(obj) {
return obj.queries.some(function(item){
return item.indexOf(filterBy) >= 0;
});
});
}

输入#1:

getResult("internet", yourObject);

输出#1:

[{"title":"Fun","url":"fun/index.html","queries":["music","artists","events","internet"]},{"title":"Internet","url":"internet/index.html","queries":["javascript","web","internet"]}]

输入#2:

getResult("music", yourObject);

输出#2:

[{"title":"Fun","url":"fun/index.html","queries":["music","artists","events","internet"]}]

关于javascript - 过滤嵌套的 JSON javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27546804/

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