Skip to content

Instantly share code, notes, and snippets.

@kylejeske
Created September 11, 2019 15:24
Show Gist options
  • Save kylejeske/7d59d2a75681a253b298b1313b330a04 to your computer and use it in GitHub Desktop.
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.
{
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