Home > Enterprise >  I need to minus one key, of array of objects, from another key of another array of objects
I need to minus one key, of array of objects, from another key of another array of objects

Time:11-29

I have next two arrays:

const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}];
const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}];

if startDate is between startDay and endDay, I have to minus firstArray number and secondArray number creating new key with result

As result, I have to put new key in firstArray with result:

const firstArray = [{startDate: 5, number: 15, result: 5}, {startDate: 25, number: 25, result: 0}];

The code I have for now:

firstArray.map(el => ({...el, result: el.number - here's number from the secondArray according to the requirements}))

CodePudding user response:

Map won't work too well for looping through two arrays and changing values.

Just use a simple for loop, check your conditions between firstArray[i] and secondArray[i], then add the value to firstArray[i].result

const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}];
const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}];

for (let i = 0; i < Math.min(firstArray.length, secondArray.length); i  )
  if (secondArray[i].startDay < firstArray[i].startDate && firstArray[i].startDate < secondArray[i].endDay)
    firstArray[i].result = firstArray[i].number - secondArray[i].number;
    
console.log(firstArray);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

When you are using two or more arrays, you need to know which index you are working with. Also, for best performance, avoid any kind callback functions, use simple loops instead:

const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}];
const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}];

for(let i = 0, first, second; i < firstArray.length; i  )
{
  first = firstArray[i];
  second = secondArray[i];
  if (first.startDate >= second.startDay && first.startDate <= second.endDay)
    first.result = first.number - second.number;
}

console.log(firstArray);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Using map() would work too and producing slightly shorter code, but it's slower and very unnecessary:

const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}];
const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}];

firstArray.map((first, i) =>
{
  const second = secondArray[i];
  if (first.startDate >= second.startDay && first.startDate <= second.endDay)
    first.result = first.number - second.number;
});

console.log(firstArray);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related