lodash を使って JavaScript でオブジェクトのコピーを行う

昨日書いたオブジェクトのコピーですが、lodashというユーティリティライブラリを使うのが手っ取り早そう

// npm install lodash -g
const _ = require("lodash");

const obj = {
    // Object のプロパティ
    name: {
        sei: "yamada",
        mei: "tarou"
    },
    age: 24
};

const other = _.cloneDeep(obj);
other.age = 17;
other.name.mei = "hanako";

console.log(obj.age);
console.log(obj.name.mei);
// output:
// 24
// tarou

これでおっけー。