Skip to content

Instantly share code, notes, and snippets.

@KyleMit
Last active July 22, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleMit/f4fd8eaf83352eac2869f24742ab3380 to your computer and use it in GitHub Desktop.
Save KyleMit/f4fd8eaf83352eac2869f24742ab3380 to your computer and use it in GitHub Desktop.
Flatten Array of Objects

Starting Point

var arr = [
  { name: "height", value:"20" },
  { name: "width",  value:"30" }
]

Target

{
  height: "20",
  width: "30"
}

Conversion

var obj = Object.assign(...arr.map((e) => ({[e.name]: e.value})))

or

var obj = arr.reduce((acc, cur) => {
  acc[cur.name] = cur.value
  return acc;
}, {})

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment