i have a array of array lets call it x
let x = [
["Last Name", "First Name", "Email Address", "Role", "Employee id (optional)"],
["Smith", "John", "[email protected]", "Employee", "ABC123XYZ"],
["Doe", "Jane", "[email protected]", "Verifier", "ABC123XYZ"]
]
how do i make it to
[
{
"Last Name": "Smith",
"First Name": "John",
"Email Address": "[email protected]",
"role": "Employee",
"Employee id": "ABC123XYZ"
},
{
"Last Name": "Doe",
"First Name": "Jane",
"Email Address": "[email protected]",
"role": "Verifier",
"Employee id": "ABC123XYZ"
}
]
how to construct a function which can return the above format
hgb123
14.9k3 gold badges24 silver badges43 bronze badges
-
Hi ben, Are you attempting to write a function that converts the above structure into the bottom structure or are you asking about how to create objects?pfych– pfych2020年08月14日 04:53:03 +00:00Commented Aug 14, 2020 at 4:53
-
i am attempting to write a functionBen Franklin– Ben Franklin2020年08月14日 05:08:48 +00:00Commented Aug 14, 2020 at 5:08
1 Answer 1
You could use map
let x = [
['Last Name', 'First Name', 'Email Address', 'Role', 'Employee id'],
['Smith', 'John', '[email protected]', 'Employee', 'ABC123XYZ'],
['Doe', 'Jane', '[email protected]', 'Verifier', 'ABC123XYZ']
]
const [props, ...data] = x
const res = data.map(d => {
const obj = {}
d.forEach((value, index) => {
obj[props[index]] = value
})
return obj
})
console.log(res)
answered Aug 14, 2020 at 4:45
hgb123
14.9k3 gold badges24 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js