Created
September 8, 2022 14:48
-
-
Save yilmazdurmaz/d1f042e1a570630f8c79ff0c0046c032 to your computer and use it in GitHub Desktop.
convert an array of data to an object with given field names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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