Lodash
Вот решение, которое использует _.flow()
:
- Преобразование объектов в массив с использованием
_.values()
, _.flatten()
,и _.compact()
(игнорировать building
при undefined
). - Преобразовать в массив
id
s с помощью _.map()
. - Получить значения с помощью
_.at()
.
const { values, flatten, compact, partialRight: pr, map, partial, at } = _;
const fn = prices => _.flow([
values,
flatten,
compact,
pr(map, 'id'),
partial(at, prices)
])
const prices = {
i_1: 'cost_i_1',
i_2: 'cost_i_2',
i_3: 'cost_i_3',
i_4: 'cost_i_4',
b_1: 'cost_b_1',
};
const features = {
improvements: [
{id: 'i_1'},
{id: 'i_2'},
{id: 'i_3'},
{id: 'i_4'},
],
building: {
id: 'b_1'
},
};
const result = fn(prices)(features);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
lodash / fp
const { values, flatten, compact, map, propertyOf } = _;
const fn = prices => _.flow([
values,
flatten,
compact,
map('id'),
map(propertyOf(prices))
])
const prices = {"i_1":"cost_i_1","i_2":"cost_i_2","i_3":"cost_i_3","i_4":"cost_i_4","b_1":"cost_b_1"};
const features = {"improvements":[{"id":"i_1"},{"id":"i_2"},{"id":"i_3"},{"id":"i_4"}],"building":{"id":"b_1"}};
const result = fn(prices)(features);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
Рамда
- Получить значения, сгладить и отфильтровать
undefined
с использованием R.identity
. - Получите реквизиты
id
с R.map
. - Используйте перевернутый
R.props
, чтобы получить значения идентификаторов из prices
const { pipe, values, flatten, filter, identity, map, prop, flip, props } = R;
const propsOf = flip(props);
const fn = prices => pipe(
values,
flatten,
filter(identity),
map(prop('id')),
propsOf(prices)
);
const prices = {"i_1":"cost_i_1","i_2":"cost_i_2","i_3":"cost_i_3","i_4":"cost_i_4","b_1":"cost_b_1"};
const features = {"improvements":[{"id":"i_1"},{"id":"i_2"},{"id":"i_3"},{"id":"i_4"}],"building":{"id":"b_1"}};
const result = fn(prices)(features);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>