Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash: deep copy object but not all properties

Is there any way to copy an object with lodash, but not all properties. The only way I know is manually copying it property by property

wanted e.g.:

var obj = {
    a: 'name',
    b: [1,2,3],
    c: {
        z: 'surname',
        x: []
    },
    d: {
        y: 'surname2',
        w: []
    }
};

and the result be like

var copy_obj = {
    b: [1,2,3],
    c: {
        z: 'surname',
        x: []
    }
};

Edit: I finally opted for:

var blacklist = ['a','d'];
_.cloneDeep(_.omit(obj, blacklist));
like image 985
fofipl Avatar asked Dec 06 '25 02:12

fofipl


1 Answers

The omit serves almost this exact purpose:

_.cloneDeep(_.omit(obj, blacklist));

Fiddle here: https://jsfiddle.net/c639m9L2/

like image 118
Chris Avatar answered Dec 08 '25 15:12

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!