Home > database >  merging several arrays into one if they have the same key inside nested object
merging several arrays into one if they have the same key inside nested object

Time:10-16

I need to create a function that sorts or filters or map multiple arrays and creates a new one

const worker1 =[{day:1, w1:2},{day:2, w1:3},{day:3, w1:1}]
const worker2 =[              {day:2, w2:1},{day:3, w2:2}]
const worker3 =[{day:1, w3:3}              ,{day:2, w3:2}]

I have 3 arrays and i need to write a function that will convert these 3 arrays into one

const total=[ {day:1, w1:2, w3:3},
              {day:2, w1:3, w2:1, w3:2},
              {day:3, w1:1, w2:2}  ]

CodePudding user response:

new solution

const worker1 = [
  { day: 1, w1: 2 },
  { day: 2, w1: 3 },
  { day: 3, w1: 1 },
];
const worker2 = [
  { day: 2, w2: 1 },
  { day: 3, w2: 2 },
];
const worker3 = [
  { day: 1, w3: 3 },
  { day: 2, w3: 2 },
];


console.log(
  sortWorkersByDate(worker1, worker2, worker3)
);
<script>
  // with ...obj you can put as many workers you want
  function sortWorkersByDate(...objWorkers) {
    let output = {};

    objWorkers.forEach((worker) => {
      worker.forEach((dayObj) => {
        ({day} = dayObj); // Destructuring assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
        if (!output[day]) output[day] = {};
        // logic for sorting
        Object.assign(output[day], dayObj);
      });
    });

    return output;
  }
</script>

basically by:

using Object.assign: we first copy the previous day's Object and then modify the correct key/value safely without the fear of deleting all the previous things

I am 100% sure that the bug is this because if you use = equal it deletes the previous and then adds the key/value which is a dangerous move.

more details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign?retiredLocale=it

so if we push w2, and previously we have w1, it will become {w1, w2} (❌ and not {w2} as = version)

the result will be:

{
  "1": {
    "day": 1,
    "w1": 2,
    "w3": 3
  },
  "2": {
    "day": 2,
    "w1": 3,
    "w2": 1,
    "w3": 2
  },
  "3": {
    "day": 3,
    "w1": 1,
    "w2": 2
  }
}

want to loop over this array (and you don't how)?

just do

Object
  .values(NAME_OF_OBJECT)
  .forEach();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values



old answer

const worker1 = [
  { day: 1, w1: 2 },
  { day: 2, w1: 3 },
  { day: 3, w1: 1 },
];
const worker2 = [
  { day: 2, w2: 1 },
  { day: 3, w2: 2 },
];
const worker3 = [
  { day: 1, w3: 3 },
  { day: 2, w3: 2 },
];

console.log(sortWorkersByDay(worker1, worker2, worker3));
<script>
  function sortWorkersByDay(...objWorkers) {
    let output = {};

    objWorkers.forEach((worker, workerIndex) => {
      worker.forEach((dayObj) => {
        const dayKey = `day${dayObj.day}`;
        const workerKey = `w${workerIndex   1}`;

        output[dayKey] = output[dayKey] || {};
        
        // or this Object.assign or it won't work
        Object.assign(output[dayKey], {
          [workerKey]: dayObj[workerKey],
        });
      });
    });

    return output;
  }
</script>

the result will be:

{
  "day1": {
    "w1": 2,
    "w3": 3
  },
  "day2": {
    "w1": 3,
    "w2": 1,
    "w3": 2
  },
  "day3": {
    "w1": 1,
    "w2": 2
  }
}
  • Related