1

I have next two arrays:

const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 10}, {startDate: 26, number: 10}];
const secondArray= [{startDay: 2, endDay:10, number: 15}, {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: 0}, {startDate: 25, number: 25, result: -10}, {startDate: 26, number: 25, result: 0}];

if I have more than one startDate in the same range(between startDay and endDay) I have to add to the last result of that range

The code I have for now:

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

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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: 10}, {startDate: 26, number: 10}];
const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}];

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

  first.result = result;
}

console.log(firstArray);

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: 10}, {startDate: 26, number: 10}];
const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}];

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

});

console.log(firstArray);

7 Comments

Can it be improved because using it with next arrays couses to mistakes const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}, {startDate: 26, number: 10}]; const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}];
I've updated the code. We can only go by the rules you've provided in your original question. You didn't mention that the arrays could be different length.
There is no result in last object of firstArray
As I said, you didn't provide any rules, therefore I have no clue what is the expected result. Your original rule state: "if startDate of firstArray is between...." well, last item does not meet that condition, because there is nothing to compare it agains. Anyhow, you get the basics, perhaps you should figure any additional conditions yourself....
const firstArray = [{startDate: 5, number: 15, result: 0}, {startDate: 25, number: 10, result: -10}, {startDate: 26, number: 10, result: 0}]; const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}]; This is result. It's like all the objects from firstArray have to plus its number if they are from the same date range ()between startDay and endDay
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.