Created
November 2, 2017 12:49
-
-
Save zymr-keshav/cbeef12adceed78e6dba96d6364a7cf6 to your computer and use it in GitHub Desktop.
es6 rest operator and arrow function with ternary operator experiments
This file contains 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
/*eslint no-unused-vars:0 */ | |
let alpha = ['a','b','c', {first: 'first'}]; | |
let beta = ['be', 'ta', { first: 'second'}]; | |
let more = {text:'more', date: new Date()}; | |
const gamma = [...alpha, more]; | |
console.log(gamma, alpha); | |
let todos = [{id: 1, completed: false}, {id:2, completed: true}]; | |
console.log("initial todos", todos); | |
function doAction (action) { | |
console.log("13 action", action); | |
todos.map( (todo, index) => { | |
console.log(todo, index); | |
if(index === action.index) { | |
console.log('17 inside if', todo); | |
// todo.completed = !todo.completed; | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}); | |
} | |
console.log('24', todo); | |
return todo | |
}) | |
} | |
let action = { index: 2 }; | |
/* trying that | |
- no curly bracket needed when using ternary in new line using arrow function | |
- can debug only when run method in console | |
- original object does not change | |
*/ | |
const nocurly = () => { | |
return todos.map( todo => | |
(todo.id === action.index) | |
? Object.assign({}, todo, { completed: !todo.completed }, { newprop:'trying more without curly'} ) | |
: todo | |
); | |
}; | |
console.log('nocurly called', nocurly()); | |
setTimeout( () => console.log('final todos', todos), 2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment