4

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

3
  • What do you mean by "how to nest this values"? Commented Dec 18, 2014 at 12:45
  • How to filter inside queries and return all object that contains this word? Commented Dec 18, 2014 at 12:48
  • If I need to filter with part of string like "mus", how can I do? Commented Dec 18, 2014 at 13:10

3 Answers 3

6

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"]}]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @kelsadita, this works, but just with the full string, I want to enter "mus" and works also...how can I did that?
Hi kelsadita, it works great!!! =) But let me ask another thing...if the JSON isn't in utf-8, with words like "Política", how can I change to utf-8 and compare? =x
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 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. Sorry this is very basic. But hope it helps.

Comments

0
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:

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"

Update

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;
    });
}

3 Comments

Isn't this very similar to the previous answer 5 mins ago?
Hi @scott-sauyet, this works, but just with the full string, I want to enter "mus" and works also...how can I did that?
@Thrustmaster, yeah, that one came in while I was writing/editing it. If this one didn't have the currying suggestion I would have just deleted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.