Skip to content

Instantly share code, notes, and snippets.

@yilmazdurmaz
Created September 8, 2022 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yilmazdurmaz/d1f042e1a570630f8c79ff0c0046c032 to your computer and use it in GitHub Desktop.
Save yilmazdurmaz/d1f042e1a570630f8c79ff0c0046c032 to your computer and use it in GitHub Desktop.
convert an array of data to an object with given field names
let fields= [ "id", "name" ]
let data=[
[ 1, 'ali' ],
[ 2, 'veli' ],
[ 3, 'deli' ]
]
let res=[]
let data.forEach( dat => res.push( {
[ fields[0] ] : dat[0],
[ fields[1] ] : dat[1]
}
))
console.log(res)
/*
[
{ id: 1, name: 'ali' },
{ id: 2, name: 'veli' },
{ id: 3, name: 'deli' }
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment