Click here to Skip to main content
15,065,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two arrays of objects. they are the months of the year.

array1 = [
        {
            date: '2020-01',
            cost: 0
        },
        {
            date: '2020-02',
            cost: 0
        },
        {
            date: '2020-03',
            cost: 0
        },
        {
            date: '2020-04',
            cost: 0
        },
        {
            date: '2020-05',
            cost: 0
        },
        {
            date: '2020-06',
            cost: 0
        },
        {
            date: '2020-07',
            cost: 0
        },
        {
            date: '2020-08',
            cost: 0
        },
        {
            date: '2020-09',
            cost: 0
        },
        {
            date: '2020-10',
            cost: 0
        },
        {
            date: '2020-11',
            cost: 0
        },
        {
            date: '2020-12',
            cost: 0
        }
    ];

array2 = [
        {
            date: '2020-01',
            cost: 10
        },
        {
            date: '2020-02',
            cost: 5
        },
        {
            date: '2020-05',
            cost: 20
        },
        {
            date: '2020-06',
            cost: 65
        },
        {
            date: '2020-07',
            cost: 11
        }
    ];

Now I want to merge them so they end up like this:

finaArray = [
        {
            date: '2020-01',
            cost: 10
        },
        {
            date: '2020-02',
            cost: 5
        },
        {
            date: '2020-03',
            cost: 0
        },
        {
            date: '2020-04',
            cost: 0
        },
        {
            date: '2020-05',
            cost: 20
        },
        {
            date: '2020-06',
            cost: 65
        },
        {
            date: '2020-07',
            cost: 11
        },
        {
            date: '2020-08',
            cost: 0
        },
        {
            date: '2020-09',
            cost: 0
        },
        {
            date: '2020-10',
            cost: 0
        },
        {
            date: '2020-11',
            cost: 0
        },
        {
            date: '2020-12',
            cost: 0
        }
    ];

Basically want to update array 1 with the content in array 2. If the dates match then update array1 with the cost I can use a libarary like loDash if it makes it easier.

What I have tried:

So far I can map over the objects:

<pre>const test = array1.map((e1, i1) => {
        array2.map((e2, i2) => {
            if (e1.date === e2.date) {
                
            }
        });
    });

But not sure how to update the array or create a new one.
Posted
Updated 13-Dec-20 22:39pm

1 solution

Try something like this:
JavaScript
const result = Array.from(array1);
array2.forEach(el => {
    let item = result.find(x => x.date === el.date);
    if (item) {
        item.cost += el.cost;
    }
    else {
        result.push(el);
    }
});
Demo[^]

NB: If you have items in array2 which don't exist in array1, they will be added to the end of the result array. If you need the elements to be in date order, you will need to sort the result array once you've finished:
JavaScript
result.sort((x, y) => x.date < y.date ? -1 : (x.date > y.date ? 1 : 0));
Array.prototype.sort() - JavaScript | MDN[^]
Sorted Demo[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900