6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryのextendで既に存在するプロパティが上書きされないようにマージする

Posted at

上書きされちゃう

var obj = {};

$.extend(obj, {
    hoge: {
        str: 'HOGE'
    }
});

$.extend(obj, {
    hoge: {
        num: 12
    }
});

console.log(obj.hoge.str); // undefined
console.log(obj.hoge.num); // 12

上書きされないようにマージさせる

var obj = {};

$.extend(true, obj, {
    hoge: {
        str: 'HOGE'
    }
});

$.extend(true, obj, {
    hoge: {
        num: 12
    }
});

console.log(obj.hoge.str); // HOGE
console.log(obj.hoge.num); // 12

第一引数に true を渡す。

参考

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?