Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript or jQuery?

This my JSON data:

[{"name":"Lenovo Thinkpad 41A4298","website":"google"}, {"name":"Lenovo Thinkpad 41A2222","website":"google"}, {"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"}, {"name":"Lenovo Thinkpad 41A424448","website":"google"}, {"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"}, {"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"}, {"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}] 

JavaScript:

obj1 = JSON.parse(jsondata); 

now I only want name and website data which is contain website is equal to "yahoo"

like image 371
Me7888 Avatar asked May 18 '14 10:05

Me7888


People also ask

What is JSON filter?

The json filter converts a JavaScript object into a JSON string. This filter can be useful when debugging your applications. The JavaScript object can be any kind of JavaScript object.

What is JSON parse in jQuery?

The jQuery parseJSON() method takes a JSON string and returns a JavaScript object. The specified JSON string must follow the strict JSON format. Passing an incorrect string will cause a JS exception. Some of the examples of malformed JSON strings that can cause an exception on passing are given as follows -

What is parse JSON in JavaScript?

A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON. parse() , and the data becomes a JavaScript object.


1 Answers

This is how you should do it : ( for google find)

$([   {"name":"Lenovo Thinkpad 41A4298","website":"google222"},   {"name":"Lenovo Thinkpad 41A2222","website":"google"}   ])     .filter(function (i,n){         return n.website==='google';     }); 

Better solution : ( Salman's)

$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {   return n.website==='google'; }); 

http://jsbin.com/yakubixi/4/edit

like image 123
Royi Namir Avatar answered Oct 05 '22 22:10

Royi Namir