Skip to content

Instantly share code, notes, and snippets.

@KyleMit

KyleMit/FlattenArray.md

Last active Jul 22, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.