Skip to content

Instantly share code, notes, and snippets.

@anacampesan
Last active June 28, 2019 14:17
Show Gist options
  • Save anacampesan/eb4e7c602be57cd3589aa8e4724f1368 to your computer and use it in GitHub Desktop.
Save anacampesan/eb4e7c602be57cd3589aa8e4724f1368 to your computer and use it in GitHub Desktop.
JS - Destructuring Array of Objects
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