Reputation: 136
I have two arrays a and b
I want to append the remarks
and delivery_status
of array a
to array b
based on latest created_date_time
of array a
.
po_number
should be used as comparing parameter. since it is a common field in both arrays
how to do this?
a=[{
actual_delivery_date: "2020-10-22"
created_date_time: "2020-10-24T22:18:29Z"
delivery_status: "Delivered"
id: 16
po_number: 1125
remarks: "test"
user: "pruser5"
},
{
actual_delivery_date: "2020-10-22"
created_date_time: "2020-10-24T22:18:29Z"
delivery_status: "Delivered"
id: 16
po_number: 1124
remarks: "test1"
user: "pruser5"
},
{
actual_delivery_date: "2020-10-29",
created_date_time: "2020-10-24T23:02:05Z",
delivery_status: "Late",
id: 22,
po_number: 1125,
remarks: "asd",
user: "pruser7"
}]
b= [{
po_num: 1125,
priority: "Medium",
processId: "30820307",
},
{
po_num: 1124,
priority: "Large",
processId: "30820308",
}]
Output I want,
b= [{
po_num: 1125,
priority: "Medium",
processId: "30820307",
delivery_status: "Delivered",
remarks: "test"
},
{
po_num: 1124,
priority: "Large",
processId: "30820308",
delivery_status: "Late",
remarks: "asd"
}]
Upvotes: 0
Views: 46
Reputation: 437
So it sounds like you only want to append parts of A onto B if firstly, they have the same po_number
, and then you only want to append the one with the latest created_date_time
.
One way to do this would be to:
created_date_time
(latest date first)find
method on A to get the entry with the same po_number as the current B entrydelivery_status
and remarks
from the found item in A, and add it to the current B entryCode:
a.sort((x, y) => y.created_date_time.localeCompare(x.created_date_time));
for (const bEntry of b) {
const matchedEntryA = a.find(aEntry => bEntry.po_num === aEntry.po_number);
if (matchedEntryA) {
bEntry.delivery_status = matchedEntryA.delivery_status;
bEntry.remarks = matchedEntryA.remarks;
}
}
It's worth noting that in your expected output example, b with po_num: 1125
has remarks: test_1
, which isn't the entry in A with the latest created_date_time
for 1125. I believe it should be remarks: asd
instead.
If you do want this code to get your expected answer, change my first line of code to: a.sort((x, y) => x.created_date_time.localeCompare(y.created_date_time));
(switched y and x in the sort compareFunction return)
It's also worth noting that if you expect A to be really long, there may be more efficient ways to do this by skipping the sort step and using a different data structure.
Hope this helped!
Upvotes: 1
Reputation: 2516
Providing you hints so that you can try yourself.
You can instantiate a map, then loop over Array a
to add po_number
as key and deliver_status
, created_date
and remarks
in a object as value. If the value is already present, then compare the created date, replace the value in map if already added date is less. Then loop over Array b
to and get value of po_num
from the map, add the deliver_status
and remarks
from map value to the item of Array b
.
You can use Array's reduce
method for iterating over Array a
and producing a map. For Array b
, you can use Array's map
method.
Upvotes: 0