0

I have the following structure and I would like to change keys name from this

let FIELDS = ['id', 'first_name', 'last_name', 'email']
let {...obj} = ['123','John','Doe', null]
console.log(obj) //  {0: '123', 1: 'John', 2: 'Doe', 3: null}

to something like this

console.log(obj) //  {id: '123', first_name: 'John', last_name: 'Doe', email: null}
0

1 Answer 1

0

I would use Object.fromEntries

const fields = ['id', 'first_name', 'last_name', 'email']
const values = ['123', 'John', 'Doe', null]

const object = Object.fromEntries(fields.map((f, i) => [f, values[i]]))

console.log(object)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.