Created
September 11, 2019 15:24
-
-
Save kylejeske/7d59d2a75681a253b298b1313b330a04 to your computer and use it in GitHub Desktop.
Using Array Spread syntax and Object Destructing -- Return the first element of an array as a property of an object.
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 elementA = { | |
name: 'xyz', | |
children: [ | |
{ name: 'abc' }, // firstChild | |
{ name: 'bcd'} // secondChild | |
] | |
}; | |
const getFirst = (d = []) => ([f, ...r] = d, f); | |
const firstChild = getFirst(elementA.children); | |
console.log(`I only want the first child: ${(firstChild === elementA.children[0] ? 'Yay! Got it.' : 'Nope.')}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment