Last active
June 28, 2019 14:17
-
-
Save anacampesan/eb4e7c602be57cd3589aa8e4724f1368 to your computer and use it in GitHub Desktop.
JS - Destructuring Array of Objects
This file contains 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 a = [{grape: 1}, {orange: 2}, {peach: 3}]; | |
let b = {}; | |
// turn b into {grape: 1, orange:2, peach: 3} | |
a.forEach((el, i) => { | |
b[Object.keys(a[i])] = el[Object.keys(el)[0]] | |
}) | |
// using object destructuring syntax | |
let [{grape}, {orange}, {peach}] = a; | |
// destructuring array of objects with default values | |
// getting first element | |
const arr = [{ | |
formSchema: { name: 'Ana'} | |
}]; | |
const [{formSchema = {}}] = [] = arr; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment