rickyduck
rickyduck

Reputation: 4084

Javascript Array/JSON Library

Basically I need some sort of library/set of functions that will allow me to perform advanced functions on arrays.

For example:

var jsondata = "SearchResponse":{ "Version":"2.0","Query":{ 
"SearchTerms":"sushi"},"Web":{ "Total":15000000,"Offset":0,"Results":[ 
{ "Title":"Sushi - Wikipedia, the free encyclopedia","Description":"In 
Japanese cuisine, sushi (寿司, 鮨, 鮓, sushi?) is vinegared rice, usually 
topped with other ingredients, including fish (cooked or uncooked) and 
vegetables.","Url":"http:\/\/en.wikipedia.org\/wiki\/Sushi","DisplayUrl
":"http:\/\/en.wikipedia.org\/wiki\/Sushi","DateTime":"2008-06-
09T06:42:34Z"}]}} /* pageview_candidate */}

var filterdata = filter(jsondata, {"Title":"Sushi - Wikipedia, the free encyclopedia"});

And then filterdata will contain all results in jsondata that have the Title Sushi - Wikipedia, the free encyclopedia

Upvotes: -1

Views: 1144

Answers (3)

Adrian Heine
Adrian Heine

Reputation: 4141

Take a look at underscore.js. You probably won’t be able to write code exactly as in your question, but it comes near to it:

_.filter(jsondata.Web.Results, function (val) {
    return val.Title === "Sushi - Wikipedia, the free encyclopedia";
});

Upvotes: 3

JonnyReeves
JonnyReeves

Reputation: 6209

Well, first of all, jsondata is an Object, not an Array; however the jsondata.Web.Results property is an Array. With that in mind, you could make use of the JavaScript Array.filter method.

var results = jsondata.Web.Results.filter(function (value) { 
    // Return true if you want this 'value' object to appear in the 'results' Array.
    return (value.Title === "Sushi - Wikipedia, the free encyclopedia");
});

If you want to do more serious work with Arrays then it would be sensible to include a Collections Framework; the most popular one for JavaScript is underscore.js. A solid understanding of Collections will pay off dividends in the future for your programming career.

Upvotes: 1

treecoder
treecoder

Reputation: 45141

Look at PHP.js. It brings PHP power to JavaScript.

Upvotes: 1

Related Questions