There are, for example, several objects in the array:

let shop = [{ id: 105, date: '2018-10-11', fact: 100, plan: 95 }, { id:105, date: '2018-10-12', fact: 105, plan: 100 }, { id:106, date: '2018-10-11', fact: 110, plan: 115 }, { id:106, date: '2018-10-12', fact: 120, plan: 115 }]; 

It is necessary to obtain a structure from objects with id and arrays of fact and plan (sales, for example):

 let groups = [{ id: 105, fact: [100, 105], plan : [95, 100] }, { id: 106, fact: [110, 120], plan: [115, 115] }]; console.log(groups); 

I tried to use reduce , but then I add up all the facts and plans without considering id. I ran the first array in forEach and map , but I also can't figure out how to leave id unique, and merge fact and plan into arrays to these id.

    2 answers 2

    • We pass on all elements of shops
    • For each item, look for an existing group by id
    • If the group is found, add the fact and plan lists to its corresponding values ​​of the element.
    • Otherwise, create a new group with values ​​from the element and add it to the list of groups.

    Implementations may differ slightly, but the point is:

     let shop = [{ id: 105, fact: 100, plan: 95 }, { id: 105, fact: 105, plan: 100 }, { id: 106, fact: 110, plan: 115 }, { id: 106, fact: 120, plan: 115 }]; let groups = []; for (let element of shop) { let existingGroups = groups.filter(group => group.id == element.id); if (existingGroups.length > 0) { existingGroups[0].fact.push(element.fact); existingGroups[0].plan.push(element.plan); } else { let newGroup = { id: element.id, fact: [element.fact], plan: [element.plan] }; groups.push(newGroup); } } console.log(groups); 

    • Thanks It works! And how in the newGroup array are filled with fact and plan, I don’t quite understand ... We are sort of passing an array, but element is not the entire data set yet? or is it overwritten? - Vasilii Maslov
    • @VasiliiMaslov on health. newGroup is a new group in which initially, in fact and plan get one value from the current element shops , yes. At the next loop iterations, this group may end up in existingGroups , being selected by id. And, accordingly, new values ​​from other elements of shops - Regent will be added to the plan and fact arrays
    • @VasiliiMaslov on your example of data: on the first iteration of the loop, there is nothing among the existing groups (what’s really there - groups generally empty), so a new group is created and added to groups . The second iteration in the existingGroups is the group created at the first iteration, because its id coincides with the id of the second element shops . Accordingly, values ​​from the second element are added to it. The third iteration leads to the creation of a new group (among the existing groups there is no group with such an id), and the fourth one - to adding a value to the group created at the third iteration - Regent

    For grouping, you can use the reduce method.

    Collapse the provided array to an object where the key is id , and eventually take the values ​​using Object.values

     let shop = [{ id: 105, date: '2018-10-11', fact: 100, plan: 95 }, { id:105, date: '2018-10-12', fact: 105, plan: 100 }, { id:106, date: '2018-10-11', fact: 110, plan: 115 }, { id:106, date: '2018-10-12', fact: 120, plan: 115 }]; var map = shop.reduce((acc, cur)=>{ acc[cur.id] = acc[cur.id] || { id: cur.id, facts: [], plans: [] }; acc[cur.id].plans.push(cur.plan); acc[cur.id].facts.push(cur.fact); return acc; },{}) var result = Object.values(map); console.log(result);