使用angularjs从不匹配的数组中过滤记录

时间:2018-02-08 09:36:38

标签: arrays angularjs

var orders = [{orderId: "1234", user_id: 222}, {orderId: "1235", user_id: 333}, {orderId: "1236", user_id: 222}, {orderId: "1237", user_id: 333}, {orderId: "1238", user_id: 334}];
var users = [{user_id: 222, name: "test1"}, {user_id: 333, name: "test2"}, {user_id: 334, name: "test3"}, {user_id: 335, name: "test4"}, {user_id: 336, name: "test5"}];


var expectResponse = [{user_id: "335", name: "test4"}, {user_id: 336, name: "test5"}]

使用angular.forloop尝试返回与订单不匹配的用户,但不起作用请指导如何修复

1 个答案:

答案 0 :(得分:0)

您可以使用像lodash.js这样的库

你可以使用differenceBy()方法

把你的json数组

_.differenceBy(orders, users, "user_id");

_.differenceBy([{"orderId": "1234", "user_id": 222}, {"orderId": "1235", "user_id": 333}], [{"user_id": 222, "name": "test1"}, {"user_id": 33, "name": "test2"}], "user_id");

检查此网址http://nginx.org/en/docs/http/ngx_http_core_module.html#internal

记得在使用lodas库之前,你需要使用lodash库。