0

I am working to filter a large json data set, I'd like to know how can I select json objects vertically.

Lat's take this small example, I'd like to select all the books with the name of author contains 'Evelyn'

data= [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price":8
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 8
      },
      { "category": "fiction",
        "author": "Evelyn Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }

as a result I should get:

  { "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 8
          },
          { "category": "fiction",
            "author": "Evelyn Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
          },

Can I do it this way:

$.each(data,function(i,el)
{
    var newdata;
     if (data.author.contains('Evelyn')) newdata.push()
});

Another way :

 data.where( "( n, i ) => n.author.contains('Evelyn') " ) ;

Do you have where is the problem in the both ways, what is the fastest ways to tackle this problem as I have a huge dataset ?

1
  • 1
    FYI. These are javascript objects, not JSON. JSON is a text interchange format. Commented Dec 4, 2013 at 8:25

4 Answers 4

4

You can use Array.filter:

var filtered = data.filter(function(a){return +(a.price) >= 8;}

Or filter on the author field:

var filtered = data.filter(function(a){return /evelyn/i.test(a.author);});
// now [filtered] contains the objects from [data] 
// where 'author' contains 'Evelyn'
// filtered.length => 2
// filtered[0].category => 'fiction'

MDN filter documentation (including a shim for older browsers)

Sign up to request clarification or add additional context in comments.

4 Comments

sorry! I edited the example, it should be "+.author.contains('Evelyn')". Does it work this way
It works for any field in the data object. See edited answer.
Might be worth mentioning that you need a polyfill for IE versions prior to IE9. Otherwise this is an excellent answer.
@BernhardHofmann: I mentioned the shim aka polyfill (follow the MDN-link, see 'Compatibility')
0

Have you tried this? I've used it in some of my projects and really can recommend it. http://www.hugoware.net/projects/jlinq

1 Comment

Seems that you are coming from CS Linq background
0

You can use filter:

data.filter(function(book) {
  return /Evelyn/.test(book.author);
});

2 Comments

I am getting boolean here . You are testing, I'd like to extract data ?
This doesn't return a boolean. It will filter all items in data where book.author contains Evelyn. If it doesn't work try posting a jsfiddle demo to reproduce the issue.
0

contains operator will not work here, you'll have to use the indexOf function here. The below code should work fine

data= [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price":8
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 8
      },
      { "category": "fiction",
        "author": "Evelyn Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }];

data.forEach(function(args){
    if(args.author.indexOf('Evelyn') >= 0){
        console.log(args);
    }

});

2 Comments

I would like to get only element that contains 'Evelyn'. Should replace console.log(args); by newdata.push(..)
it's already getting the elements that contain 'Evelyn', however it's not pushing them into an array. If you want to push them over an array you'll have to use newdata.push(args) considering that the array's name is newdata

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.