Combining objects from two list of objects

I have two lists of objects as outlined below. I want to combine them by the FormattedDate such that the OrderSum is added together.

I can think of a brute-force way to do this by iterating over list one, and for each iteration, iterating the entire list two, basically a nested loop, but that seems inefficient as I have to do it over lists of 20-30 items.

Thank you!

List 1

[
	{
		"___class": "Orders",
		"OrderSum": 1115,
		"FormattedDate": "10/4/2022",
		"objectId": "2452620B-E2A4-4CE7-822C-DA2C9664A6B9"
	},
	{
		"___class": "Orders",
		"OrderSum": 693,
		"FormattedDate": "10/5/2022",
		"objectId": "5EFC1C85-21D2-4445-BCD6-2A9FD0F87951"
	},
	{
		"___class": "Orders",
		"OrderSum": 24,
		"FormattedDate": "10/6/2022",
		"objectId": "31ED6957-35A1-4C6E-85F6-33F451F52E3C"
	}
]

List 2

[
	{
		"___class": "Orders",
		"OrderSum": 25,
		"FormattedDate": "10/5/2022",
		"objectId": "648CBF6C-8638-4625-B10A-3DCA84FA77A7"
	}
]

Output List

[
	{
		"___class": "Orders",
		"OrderSum": 1115,
		"FormattedDate": "10/4/2022",
		"objectId": "2452620B-E2A4-4CE7-822C-DA2C9664A6B9"
	},
	{
		"___class": "Orders",
		"OrderSum": 718, ***** this value was updated *****
		"FormattedDate": "10/5/2022",
		"objectId": "5EFC1C85-21D2-4445-BCD6-2A9FD0F87951"
	},
	{
		"___class": "Orders",
		"OrderSum": 24,
		"FormattedDate": "10/6/2022",
		"objectId": "31ED6957-35A1-4C6E-85F6-33F451F52E3C"
	}
]

Hello @Tim_Jones,

I can suggest a quick solution using a custom code block, the logic is following:

const list1ByDate = {}

for (const item of list1) {
  list1ByDate[item.FormattedDate] = item
}

for (const item of list2) {
  if (list1ByDate[item.FormattedDate]) {
    list1ByDate[item.FormattedDate].OrderSum += item.OrderSum
  } else {
    list1ByDate[item.FormattedDate] = item
  }
}

return Object.values(list1ByDate)

(naming of variables is up to you)

Would you like to see this using codeless blocks?

Thank you! I would also appreciate an example using codeless. The two lists in my example are coming from the DB and I need to merge them.

Tim

Could you please clarify, if some item in List2 has FormattedDate that does not exist in List1, should it be added to List1 or ignored?

Good question. It should be added.

Here you go:

This is a custom function that accepts two arguments: list1 and list2. All namings are up to you as usual.

Please let me know if you have any questions regarding this code.

Wow! That worked insanely well. My API had a single query with a WHERE OR statement, and it was slow -

4 test runs
Processing finished in 4079.123ms
Processing finished in 4418.669ms
Processing finished in 3254.863ms
Processing finished in 3964.053ms

After splitting the query into two and merging the returned lists with the function you provided -

4 test runs
Processing finished in 561.414ms
Processing finished in 291.237ms
Processing finished in 306.180ms
Processing finished in 295.455ms

Good lesson learned. Backendless doesn’t do fast queries with ORs.

I forgot to say it. Thank you! This was a huge help.