copyDiff lodash

node v14.6.0
version: 1.0.0
endpointsharetweet
var _ = require("[email protected]")
function copyDiff(source, ...diffs) { const sourceDiffs = diffs.map(([key, value]) => _.set({}, key, value)); return _.merge({}, source, ...sourceDiffs); } const order = {require("lodash") id: '123', checkout: { delivery: { deliveryFee: 10 }, coords: { lat: 1, lon: 2 }, } }; // original console.log(order) // returns a new order with id 456 console.log(copyDiff(order, ['id', '456'])) // returns a new order with free delivery console.log(copyDiff(order, ['checkout.delivery.deliveryFee', 0])); // returns a new order with id 678, delivery fee of 50 cents, and latitude 3 console.log(copyDiff(order, ['id', '678'], ['checkout.delivery.deliveryFee', 0.5], ['checkout.coords.lat', 3]));
Created from: https://lodash.com/docs/4.17.15#merge
Loading…

no comments

    sign in to comment