开发者

Javascript [jQuery] filter array by key and value

I need to filter an array if a key meets a certain value, for example:

data = theArray;
v开发者_StackOverflowar theColumn = "thc012";
var theVal = 4;
data = $.filter(data, theColumn, theVal );

And then the result would be all the array items that match theColumn = theVal ;

I am already using the jQuery Library so if any jQuery functions help, use them.

Thanks


Javascript objects are key-value pairs. You can use them. Like so:

var data = {
    "cars": ["Honda", "Toyota", "Subaru"],
    "planes": ["Boeing", "Airbus", "Mig"]
};
var key = "cars";
data[key]; // All cars
key = "planes";
data[key]; // All planes


I think you're looking for the grep method: jQuery.grep

Then just supply it with a function that checks if theColumn = theKey;


if i have understood your question correctly this should do it

 $.each(data,function(row,key){ 
    if(key != theKey && theColumn != theColumn){
        delete data[key];
    } 
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜