There is such an object:

Object {managers: Object, groups: Object} groups{ group_0:"Отдел продаж" group_20691:"Отдел продаж 2" group_22578:"Отдел продаж 3" group_38934:"Производство" group_40083:"PR отдел" } managers{ 275198:{ active:true group:"group_40083" id:"275198" is_admin:"Y" login:"kolpa4kov@gmail.com" option:"Алексей Колпаков Get8" status:"OK" title:"Алексей Колпаков Get8" } 306202:{ active:true group:"group_0" id:"306202" is_admin:"Y" login:"altastra@gmail.com" option:"Александра" status:"OK" title:"Алексей Колпаков Get8" } ... } 

where in groups keys group_0,group_20691 , etc. equal to the managers['group'] . We need to form such an object.

 manager{ ["PR отдел"]=array({'group_40083','Алексей Колпаков GET8'},{'...','...'},...) ["Отдел продаж"]=array({'group_0','Иван'},{'...','...'},...)........ } 

where managers will be broken down by department

  • Give an example of an object with text, maybe not all 200 properties. And also explain what kind of array you need to convert to the array, the code is incomprehensible. Either you want several arrays, or you really need not an array, but an object with fields - Grundy
  • phpmix ... @Grundy, he wants an object in which the keys are the names of groups, and the values ​​are arrays of something like those objects that are now members (perhaps not with all the fields, perhaps as an array of two elements). But with this quality of question, I have absolutely no desire to help. - Qwertiy
  • @Qwertiy, well, I roughly understood what was needed there, but I had to drive these objects myself to try - somehow laziness - Grundy
  • I'll fix it now - Ivan Triumphov

2 answers 2

 $.ajax({ url:"/ajax/get_managers_with_group/", type:"POST", data:{}, dataType:"json" }).success(function(result) { var managers = {}; for (groupId in result.groups) { managers[result.groups[groupId]] = []; for (managerId in result.managers) { if (result.managers[managerId].group == groupId) { managers[result.groups[groupId]].push(result.managers[managerId]); } } } console.log(managers); }); 
  • if you run directly to the managers, then you don’t need to run to the groups, you can simply apply the necessary property :) - Grundy

Since the direct match and the group field of the manager correspond to the key in the groups object, to solve it, it is enough to run through all the managers to get across the field - the name of the group, it will also be the field of the final object and save the current object.

For example:

 var manager = Object.values(data.managers).reduce(function(acc, cur){ var group = data.groups[cur.group]; if(!acc[group]) acc[group]=[]; //acc[group].push(cur); // если нужен полностью объект менеджера //acc[group].push([cur.group,cur.title]); //если значением должен будть массив acc[group].push({[cur.group]:cur.title});//для объектов вида {'group_40083':'Алексей Колпаков GET8'} return acc; },{}); 
  • Grundy thanks a lot for the answer, I will not delve into it. But I managed to do it already - Ivan Triumphov