0

I wanted to ask, how can i filter data using Jquery and grep?

Some example code:

var arr = ['11','21']

obj = $.grep(obj, function (element, index) {
        return element.Id == 11;
    });

This will return obj data with id of 11. But, how can i retrieve data from obj, when Id will be like arr array ?

So i wanted, to have obj with properties equal to Id 11 and 21 ?

Thank You.

1
  • So it is like an or conditional? Id == arr[0] || Id == arr[1] ? Commented May 22, 2015 at 11:25

2 Answers 2

1

Use Array.indexOf()

var arr = ['11', '21']

obj = $.grep(obj, function (element, index) {
    return arr.indexOf(element.Id + '') > -1;//the + '' is used since the array has string values
    //or $.inArray(element.Id + '', arr) > -1
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use JavaScript's own native filter method to achieve the same result:

var arr = obj.filter(function (el) {
    return arr.map(Number).indexOf(el.Id) > -1;
});

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.