.
"use strict";
function correlate(
outer,
inner,
outerKeyOrKeySelector = x => x,
innerKeyOrKeySelector = x => x
) {
const outerValues = [...outer];
const innerValues = [...inner];
const outerToKey = typeof outerKeyOrKeySelector === 'function'
? outerKeyOrKeySelector
: (x => x[outerKeyOrKeySelector]);
const innerToKey = typeof innerKeyOrKeySelector === 'function'
? innerKeyOrKeySelector
: (x => x[innerKeyOrKeySelector]);
const outerKeyed = outerValues.map(value => ({key: outerToKey(value), value});
const innerKeyed = innerValues.map(value => ({key: innerToKey(value), value});
return outerKeyed.reduce((results, {key: oKey, value: oValue}) => [
...results,
...innerKeyed
.filter(({key: iKey}) => oKey === iKey)
.map(({value: iValue}) => [oValue, iValue])
], []);
}
JOIN .
, YMMV, .
:
reArr = correlate(orderArr, myArr).map(([first, second]) => first);
reArr = correlate(orders, customers, o => o.customer.name, c => c.name)
.map(([first, second]) => {order: first, customer: second})
.forEach(({customer, order}) => {
customer.orders = [...customer.orders, order];
});