0

I have an list as,

$scope.list1= [{"name":"name1","id":1},{"name":"name2","id":2},{"name":"name3","id":3},{"name":"name4","id":4}];

I want to filter this list with respect to another list as,

$scope.list2=[1,3];

Here I want filter list1 so that only those objects items are left ehich are in list2. i.e

after filter

$scope.list1= [{"name":"name1","id":1},{"name":"name3","id":3}];

I can do this by using the splice function . But I want to ask that whether this can be done using the $filter without using a loop.

1
  • How about underscore.js? I like it a lot for doing those kind of tasks without having loops. Commented Apr 1, 2015 at 9:49

1 Answer 1

0

You can easily do that with the open source project jinqJs http://www.jinqJs.com

See Fiddle

var list1= [{"name":"name1","id":1},{"name":"name2","id":2},{"name":"name3","id":3},{"name":"name4","id":4}];
var list2= [1,3];

var result = jinqJs().from(list1).where(function(row){
    return (list2.indexOf(row.id) > -1);
}).select();

OR

this way

//Use jsJinq.com open source library
var list1= [{"name":"name1","id":1},{"name":"name2","id":2},{"name":"name3","id":3},{"name":"name4","id":4}];
var list2= [1,3];

var result = jinqJs().from(list1).join(list2).on('id').select();

document.body.innerHTML += '<pre>' + JSON.stringify(result, null, 4) + '</pre>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>

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

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.