|
| 1 | +const houses = ['Arryn', 'Frey', 'Greyjoy', 'Stark', 'Lannister', 'Tyrell']; |
| 2 | + |
| 3 | +// There are a few non-destructive methods available for arrays. |
| 4 | +// One example is slice. It returns a piece of a given Array |
| 5 | +// without modifiyng the original one. |
| 6 | + |
| 7 | +console.log(houses.slice(0, 4)); // ['Arryn', 'Frey', 'Greyjoy', 'Stark'] |
| 8 | + |
| 9 | +console.log(houses); // ['Arryn', 'Frey', 'Greyjoy', 'Stark', 'Lannister', 'Tyrell'] |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +// Besides slice, there are other and useful array methods |
| 14 | +// such as filter, map and reduce that also returns new arrays |
| 15 | +// instead of modifying the original one. |
| 16 | + |
| 17 | +const direwolves = [ |
| 18 | + { name: 'Ghost', alive: true }, |
| 19 | + { name: 'Nymeria', alive: true }, |
| 20 | + { name: 'Lady', alive: false }, |
| 21 | + { name: 'Grey Wind', alive: false }, |
| 22 | + { name: 'Shaggydog', alive: false }, |
| 23 | + { name: 'Summer', alive: false } |
| 24 | +] |
| 25 | + |
| 26 | +const wolves = direwolves.filter(wolf => wolf.alive) |
| 27 | + |
| 28 | +console.log(direwolves); //Full Array |
| 29 | +console.log(wolves); // [{name:'Ghost', alive: true},{name:'Nymeria', alive:true}] |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +// Javascript ES6 introduces a new operator called the spread operator. |
| 34 | +// The Spread operator provides an easy way to create a new array |
| 35 | +// by copying values from another array. |
| 36 | + |
| 37 | +const completeHouses = [...houses, 'Targaryen']; |
| 38 | +console.log(houses) // ['Arryn','Frey','Greyjoy','Stark','Lannister','Tyrell'] |
| 39 | +console.log(completeHouses) //['Arryn','Frey','Greyjoy','Stark','Lannister','Tyrell','Targaryen'] |
| 40 | + |
| 41 | +// There is a proposal for object spread syntax for the next version |
| 42 | +// of the JS language, and you can use it right now with Babel. |
| 43 | + |
| 44 | +const state = { |
| 45 | + name: 'Jon Snow', |
| 46 | + occupation: 'Lord Commander', |
| 47 | + skills: [] // knows nothing... |
| 48 | +} |
| 49 | + |
| 50 | +// Now, using the spread operator I will copy the original state objects keys |
| 51 | +// and values, while at the same changing the occupation value. |
| 52 | + |
| 53 | +const newState = { ...state, occupation: 'King in the North' }; |
| 54 | +console.log(newState); |
| 55 | +// {name:'Jon Snow', occupation:'King in the North', skills:[]} |
| 56 | +console.log(state); |
| 57 | +// {name:'Jon Snow', occupation:'Lord Commander', skills:[]} |
| 58 | + |
| 59 | + |
| 60 | +// The spread operator makes shallow copies, which means |
| 61 | +// it only goes one level deep while copying. |
| 62 | +// If you want to update the array, you will need to make it |
| 63 | +// in an immutable fashion as well: |
| 64 | + |
| 65 | +newState.skills = [...state.skills, 'fighting']; |
| 66 | +console.log(newState); |
| 67 | +// {name:'Jon Snow', occupation:'King in the North', skills:[fighting]} |
| 68 | +console.log(state); |
| 69 | +// {name:'Jon Snow', occupation:'Lord Commander', skills:[]} |
0 commit comments