I got two lists of objects :
let list1 = [{id: '1', status: 'use', comment: 'xxxx'}, {id: '2', status: 'ready', comment: 'yyyy'}, {id: '3', status: 'ready', comment: 'zzzz'}];
let list2 = [{uid: '1', elec: 60}, {uid: '2', elec: 60}, {uid: '10', elec: 60}, {uid: '3', elec: 40}];
What i want is to retrieve an object of list2 that have elec > 50 and the same uid than one item id of the list1 only if the item of the list1 have a status == "ready". Also, i want to add to this item the parameter 'comment' from the object of the list1.
In this exemple, my result value would be : {uid: '2', elect: 60, comment: 'yyyy'}.
I did this :
let list1Filtered = list1.filter(itemList1 => itemList1.status == 'ready');
let list2Filtered = list2.filter(itemList2 => itemList2.elec > 50);
var result;
for ( let itemList1Filtered of list1Filtered ) {
for ( let itemList2Filtered of list2Filtered ) {
if (!result && itemList1Filtered.id == itemList2Filtered.uid) {
result = itemList2Filtered;
result.comment = itemList1Filtered.comment;
}
}
}
return result;
I want to know if there is a more elegant and/or more sophisticated way to do this in Javascript.