1

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

2
  • 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? Commented Aug 14, 2020 at 4:53
  • i am attempting to write a function Commented Aug 14, 2020 at 5:08

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.