Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myObject = {
- name: 'myObject',
- number: 9001,
- nestedObject: {
- nestedName: 'nestedName',
- nestedBasicArray: [1,2,3],
- nestedObjectArray: [{firstThing: 11, secondThing: 12}, {firstThing: 21, secondThing: 22}, {firstThing: 31, secondThing: 32}]
- }
- }
- var {
- name,
- number,
- nestedObject: {
- nestedName,
- nestedBasicArray: [a, b, c],
- nestedObjectArray: [...d]
- }
- } = myObject
- // These values have the same key name in our object, so we declare and call them directly
- console.log(`Name is ${name}`)
- console.log(`Name is ${number}`)
- console.log(`Nested Name is ${nestedName}`)
- // Here we use some basic pattern matching
- console.log(`a is ${a}`)
- console.log(`b is ${b}`)
- console.log(`c is ${c}`)
- // We use the object spread '...' operator to deconstruct this array of objects
- console.log('d is:')
- console.log(d);
- // Let's use an example function to dig a little deeper
- d.map(({firstThing, secondThing}) => {
- console.log(`firstThing is ${firstThing}`);
- console.log(`secondThing is ${secondThing}`);
- })
Add Comment
Please, Sign In to add comment