Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  62] [ 6]  / answers: 1 / hits: 8675  / 10 Years ago, thu, december 18, 2014, 12:00:00

I'm creating an API that takes a JSON like this:



 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.



 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...


More From » json

 Answers
1

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:



getResult(internet, yourObject);


Output #1:



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


Input #2:



getResult(music, yourObject);


Output #2:



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

[#40539] Wednesday, December 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alora

Total Points: 284
Total Questions: 99
Total Answers: 92

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;