Building objects from nested arrays
JavaScriptAll snippets in this category →
Posted on We can turn nested arrays of key-value-pairs into an object with Object.fromEntries()
.
const pairs = [
["name", "Bob"],
["occupation", "Fry cook"],
["shape", "rectangular"]
]
Object.fromEntries(pairs)
// ⇒ {
// name: "Bob",
// occupation: "Fry cook",
// shape: "rectangular"
// }
const pairs = [
["name", "Bob"],
["occupation", "Fry cook"],
["shape", "rectangular"]
]
Object.fromEntries(pairs)
// ⇒ {
// name: "Bob",
// occupation: "Fry cook",
// shape: "rectangular"
// }
const pairs = [
["name", "Bob"],
["occupation", "Fry cook"],
["shape", "rectangular"]
]
Object.fromEntries(pairs)
// ⇒ {
// name: "Bob",
// occupation: "Fry cook",
// shape: "rectangular"
// }
const pairs = [
["name", "Bob"],
["occupation", "Fry cook"],
["shape", "rectangular"]
]
Object.fromEntries(pairs)
// ⇒ {
// name: "Bob",
// occupation: "Fry cook",
// shape: "rectangular"
// }