LoginSignup
3
1

More than 5 years have passed since last update.

jsのhash(Object)をmergeする

Posted at

jsのhash(Object)をmergeする方法をたまに忘れちゃうのでメモ。

Object.assignを使う

test = {x: 1, y: 2, a: 3, b: 4}
console.log(Object.assign(test, {c: 5}))

> {x: 1, y: 2, a: 3, b: 4, c: 5}

3dots(Spread Operator)を使う

test = {x: 1, y: 2, a: 3, b: 4}
console.log({...test, c: 5})

> {x: 1, y: 2, a: 3, b: 4, c: 5}

Lodash/Underscoreを使う

test = {x: 1, y: 2, a: 3, b: 4}
console.log(_.merge(test, {c: 5}))

> {x: 1, y: 2, a: 3, b: 4, c: 5}
3
1
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
3
1