[英]Compare two object arrays and combine missing objects
I have 2 object arrays. 我有2个对象数组。 The 1st is an array of managers.
第一个是一组经理。 The 2nd is an array of selected managers from the 1st array.
第二个是从第一个数组中选择的管理者组成的数组。 The difference is I added a property
selected: true
. 区别在于我添加了一个
selected: true
的属性selected: true
。 I need to now replace the the managers in the first array with selected managers. 现在,我需要用选定的管理器替换第一个阵列中的管理器。 I am doing this with an AngularJS service I created.
我正在使用我创建的AngularJS服务来执行此操作。 I'm sure there is much simpler solution so I'm open to suggestions.
我敢肯定有更简单的解决方案,所以我愿意提出建议。 JavaScript, jQuery, lodash, LINQ.js are good.
JavaScript,jQuery,lodash,LINQ.js都不错。 I have a plunker and I have displayed the result I need.
我有一个小矮人,并且已经显示了所需的结果。 Notice the manager that does not have the
selected:true
property. 请注意,该管理器没有
selected:true
属性。
var app = angular.module("mainModule", []);
var MainController = function($scope, service) {
var eventUsers = [
{
"event_Users_ID":1009,"event_ID":11,"user_ID":"15e640c1-a481-4997-96a7-be2d7b3fcabb"
},{
"event_Users_ID":1010,"event_ID":11,"user_ID":"250a19be-e661-4c04-9a50-c84b0e7349b7"
},{
"event_Users_ID":1011,"event_ID":11,"user_ID":"4cada7f0-b961-422d-8cfe-4e96c1fc11dd"
},{
"event_Users_ID":1013,"event_ID":11,"user_ID":"a3125317-5deb-426d-bbb1-06d3bd4ebaa6"
}];
var managers = [
{
"id": "15e640c1-a481-4997-96a7-be2d7b3fcabb",
"fullName": "Kul Srivastva"
},{
"id": "250a19be-e661-4c04-9a50-c84b0e7349b7",
"fullName": "Todd Brothers"
}, {
"id": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd",
"fullName": "Rudy Sanchez"
}, {
"id": "79823c6d-de52-4464-aa7e-a15949fb25fb",
"fullName": "Mike Piehota",
}, {
"id": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6",
"fullName": "Nick Broadhurst"
}];
$scope.result = service.eventUserMatch(eventUsers, managers);
};
function service() {
var vm = this;
vm.eventUserMatch = function (eventUsers, managers) {
var arry = [];
arry = $.map(eventUsers, function (eventUser) {
var manager = $.grep(managers, function (user) {
return user.id === eventUser.user_ID;
})[0];
eventUser.id = manager.id;
eventUser.fullName = manager.fullName;
eventUser.selected = true;
return eventUser;
});
return arry;
};
}
app.controller("MainController", MainController);
app.service('service', service);
You can use Array#map
. 您可以使用
Array#map
。
// Get all the event user IDs in an array
var eventUserIds = eventUsers.map(e => e.user_ID);
// Iterate over managers
managers = managers.map(e => {
// If manager is present in the event users, `select` it
if (eventUserIds.indexOf(e.id) !== -1) {
e.selected = true;
}
return e;
});
var eventUsers = [{ "event_Users_ID": 1009, "event_ID": 11, "user_ID": "15e640c1-a481-4997-96a7-be2d7b3fcabb" }, { "event_Users_ID": 1010, "event_ID": 11, "user_ID": "250a19be-e661-4c04-9a50-c84b0e7349b7" }, { "event_Users_ID": 1011, "event_ID": 11, "user_ID": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd" }, { "event_Users_ID": 1013, "event_ID": 11, "user_ID": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6" }]; var managers = [{ "id": "15e640c1-a481-4997-96a7-be2d7b3fcabb", "fullName": "Kul Srivastva" }, { "id": "250a19be-e661-4c04-9a50-c84b0e7349b7", "fullName": "Todd Brothers" }, { "id": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd", "fullName": "Rudy Sanchez" }, { "id": "79823c6d-de52-4464-aa7e-a15949fb25fb", "fullName": "Mike Piehota", }, { "id": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6", "fullName": "Nick Broadhurst" }]; var eventUserIds = eventUsers.map(e => e.user_ID); managers = managers.map(e => { if (eventUserIds.indexOf(e.id) !== -1) { e.selected = true; } return e; }) console.log(managers); document.getElementById('result').innerHTML = JSON.stringify(managers, 0, 4);
<pre id="result"></pre>
As you said, I do think there may be an easier way to do this. 正如您所说,我确实认为可以有一种更简单的方法。
I advise you to pick a look to SugarJs which is a JavaScript library that extends native objects with so helpful methods. 我建议您看一下SugarJs ,它是一个JavaScript库,它使用非常有用的方法扩展了本机对象。 In your case the doc on Arrays .
您的情况下Arrays上的文档。
For me, it helps a lot dealing with a lot of native JavaScript Object (JSON). 对我来说,它可以处理很多本机JavaScript对象(JSON)。
would this work? 这会工作吗? Loop through the new array of managers, find the index using lodash of a matching manager object in the old manager array and replace it in the old manager array with the manager from the new manager array if found?
遍历新的管理器数组,使用旧管理器数组中匹配的管理器对象的破折号查找索引,然后在新管理器数组中用新管理器数组中的管理器替换旧管理器数组中的索引(如果找到)?
There's probably a more efficient way to write a solution to this but assuming I'm understanding your problem correctly I believe this should work? 可能有一种更有效的方法来为此编写解决方案,但是假设我正确地理解了您的问题,我相信这应该可行吗? Can't test as I'm at work currently.
无法测试,因为我目前正在工作。
for(var i=0; i < SelectedManagersArray.length; i++){
var index = _.findIndex(OldManagersArray, {id: SelectedManagersArray[i].id, fullName: selectedManagersArray[i].fullName);
//I believe lodash returns a -1 if a matching index isn't found.
if(index !== -1){SelectedManagersArray[index] = OldManagersArray[i]}
}
Simple implementation: 简单实施:
for(var i = 0; i < eventUsers.length; i++) {
for(var j = 0; j < managers.length; j++) {
if(eventUsers[i].user_ID === managers[j].id) {
managers[j].selected = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.