1

I'm trying to filter an array, from another array :

I have the first array :

var json = [["ID1", "Person1", "10"], ["ID2", "Person2", "20.0"], ["ID3", "Person3", "50.0"], ["ID4", "Person4", "40.0"]];

My filter array : (which is not ordered);

var filter = ["ID4", "ID1"]; 

And the result I would like to have :

var json = [["ID1", "Person1", "10"], ["ID4", "Person4", "40.0"]];

or

var json = [["ID4", "Person4", "40"], ["ID1", "Person1", "10.0"]];
1
  • Or just plain old iteration -> jsfiddle.net/NWQJV Commented Jun 14, 2013 at 12:56

1 Answer 1

2

You could do

json = json.filter(function(v) { return filter.indexOf(v[0])!==-1 })

But I'd recommend you to avoid naming json a variable not holding some JSON.

If you want to be compatible with IE8, I'd recommend to use a shim for filter and indexOf or simply to iterate using 2 for loops.

Sign up to request clarification or add additional context in comments.

2 Comments

That's the best solution for newer browsers, but not supported in IE8 or below. +1, but it looks strange with variable names like json and filter !
It works great thanks! I'll check the alternative for IE8, since I'll need it. Thanks!

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.