[英]Filtering nested JSON javascript
I'm creating an API that takes a JSON like this: 我正在创建一个采用JSON的API,如下所示:
"hightlights":[
{
"title":"Fun",
"url":"fun/index.html",
"queries":
[
"music",
"artists",
"events",
"internet"
]
},
{
"title":"Internet",
"url":"internet/index.html",
"queries":
[
"javascript",
"web",
"internet",
]
}
]
I need to filter the JSON with a word given by user and return with another JSON with only object that contains the word in "queries". 我需要使用用户提供的单词过滤JSON,然后返回另一个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",
]
}
My problem is how to nest this values? 我的问题是如何嵌套此值? If anyone can give me some example...I'll appreciate... 如果有人能给我一些例子...我将不胜感激...
Try the below: 请尝试以下方法:
function getResult(filterBy, objList) {
return objList.hightlights.filter(function(obj) {
return obj.queries.some(function(item){
return item.indexOf(filterBy) >= 0;
});
});
}
Input#1: 输入#1:
getResult("internet", yourObject);
Output #1: 输出#1:
[{"title":"Fun","url":"fun/index.html","queries":["music","artists","events","internet"]},{"title":"Internet","url":"internet/index.html","queries":["javascript","web","internet"]}]
Input #2: 输入#2:
getResult("music", yourObject);
Output #2: 输出#2:
[{"title":"Fun","url":"fun/index.html","queries":["music","artists","events","internet"]}]
var search = function(data, query) {
return data.hightlights.filter(function(highlight) {
return highlight.queries.indexOf(query) > -1;
});
}
search(data, "music");
If you have a curry
function available (as from Ramda , Lo-Dash , etc.) you can wrap this: 如果您有可用的curry
功能(例如Ramda , Lo-Dash等),则可以将其包装:
var search = curry(function(data, query) { ... });
And then you can create simpler functions by passing in the data first: 然后,您可以通过首先传入数据来创建更简单的函数:
highlights = search(data);
//...
highlights("music"); //=> those containing "music"
highlights("internet"); //=> those containing "internet"
I know there is already a working solution. 我知道已经有一个可行的解决方案。 But this might be a bit simpler. 但这可能会更简单。
This would search inside the queries for subwords: 这将在查询中搜索子词:
var search = function(data, query) {
return data.hightlights.filter(function(highlight) {
return highlight.queries.filter(function(q) {
return q.indexOf(query) > -1;
}).length > 0;
});
}
var xyz = {
"hightlights":[
{
"title":"Fun",
"url":"fun/index.html",
"queries":[
"music",
"artists",
"events",
"internet"
]
},
{
"title":"Internet",
"url":"internet/index.html",
"queries":[
"javascript",
"web",
"internet",
]
}
]
}
no lets say "var word" contains the keywords to be searched, then you could simply do this: 不能说“ var word”包含要搜索的关键字,那么您可以简单地做到这一点:
var arr = [];
for(var i in xyz['highlights']){
var queries = xyz['highlights']['queries'];
for(j in queries){
if (word == queries[j]){
arr.push(xyz['highlights'][i]);
continue;
}
}
}
arr should have your required object. arr应该具有您所需的对象。 Sorry this is very basic. 抱歉,这是非常基本的。 But hope it helps. 但希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.