我有一个列表,
$scope.list1= [{"name":"name1","id":1},{"name":"name2","id":2},{"name":"name3","id":3},{"name":"name4","id":4}];
我想根据另一个列表过滤此列表,
$scope.list2=[1,3];
这里我想要过滤器list1,以便只留下列表2中的那些对象项目。即
过滤后
$scope.list1= [{"name":"name1","id":1},{"name":"name3","id":3}];
我可以通过使用拼接功能来完成此操作。但我想询问是否可以使用$ filter而不使用循环来完成。
答案 0 :(得分:0)
您可以使用开源项目jinqJs http://www.jinqJs.com
轻松完成此操作请参阅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
这样
//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>';
&#13;
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>
&#13;