Javascript Array/JSON Library

Javascript Array/JSON Library

本文关键字:Library JSON Array Javascript      更新时间:2023-09-26

基本上,我需要某种库/函数集,允许我在数组上执行高级函数。

例如:

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

然后filterdata将包含jsondata中标题为Sushi - Wikipedia, the free encyclopedia

的所有结果

看一下underscore.js。您可能无法编写与您的问题完全相同的代码,但它接近于它:

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

首先,jsondata是一个对象,而不是一个数组;然而,jsondata.Web.Results属性是一个数组。考虑到这一点,您可以使用JavaScript数组。过滤方法。

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

如果你想对数组做更严肃的工作,那么包含一个集合框架将是明智的;JavaScript中最流行的是underscore.js。对集合的深刻理解将为你未来的编程生涯带来好处。

看看PHP.js。