1

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

data = theArray;
var 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

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

2 Comments

Ok, that looks like it works great, however I am passing my data dynamically, so for example the key is set in a variable. This means it I do el.theColumn, with theColumn being the variable with the key name, and el, being the array, it in fact looks for the key name theColumn, instead of what the value of the variable theColumn is, if this makes sense?
I don't think I understand. Can you give an example with actual values?
0

if i have understood your question correctly this should do it

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

Comments

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.