javascript - Map objects together in JS (es6) -
i have 2 objects in js:
positions: { top: { top: 10, left: 10 }, bottom: { top: 10, left: 10 }, left: { top: 10, left: 10 }, right: { top: 10, left: 10 }, }, positionadjustments: { top: { top: 0, left: 0 }, bottom: { top: 5, left: 5 }, left: { top: 0, left: 0 }, right: { top: 0, left: 0 }, },
i want combine them, produce example:-
positions: { top: { top: 10, left: 10 }, bottom: { top: 15, left: 15 }, left: { top: 10, left: 10 }, right: { top: 10, left: 10 }, },
is there lodash method or elegant way achieve without writing recursive mapping function? eg:
positions.deepcombinevalues(positionadjustments);
you iterate keys , wanted properties , adjust positions.
var object = { positions: { top: { top: 10, left: 10 }, bottom: { top: 10, left: 10 }, left: { top: 10, left: 10 }, right: { top: 10, left: 10 } }, positionadjustments: { top: { top: 0, left: 0 }, bottom: { top: 5, left: 5 }, left: { top: 0, left: 0 }, right: { top: 0, left: 0 } } }; object.keys(object.positions).foreach(function (k) { ['top', 'left'].foreach(function (l) { object.positions[k][l] += (object.positionadjustments[k] || {})[l] || 0; }); }) console.log(object.positions);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Comments
Post a Comment