I am currently available for freelance/contract work. Book a meeting so we can talk about your project.

Transforming flat arrays into object arrays

Posted on

If an array contains flat values, we can quickly turn those into objects by mapping over them. The name we give them in the call to Array.prototype.map() becomes the name of the objects’ property when we do it like this.

// turn these strings into objects, where they become `name`-properties
["John", "Paul", "George", "Ringo"].map(name => ({ name }))
// ⇒ [
//     { name: "John"   },
//     { name: "Paul"   },
//     { name: "George" },
//     { name: "Ringo"  }
//   ]
// turn these strings into objects, where they become `name`-properties
["John", "Paul", "George", "Ringo"].map(name => ({ name }))
// ⇒ [
//     { name: "John"   },
//     { name: "Paul"   },
//     { name: "George" },
//     { name: "Ringo"  }
//   ]
Debug
none
Grid overlay