Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. let character = {};
  2. updateCharacter = (path, value) => {
  3. character[path] = value;
  4. }
  5.  
  6. const Character = function() {
  7. this.moves = [];
  8. this.firstName = ''
  9. this.lastName = ''
  10.  
  11. this.getName = () => `${this.firstName} ${this.lastName}`;
  12. this.setFirstName = name => this.firstName = name;
  13. this.setLastName = name => this.lastName = name;
  14.  
  15. this.addMove = (name, notatian) => {
  16. this.moves.push({ name, notatian });
  17. }
  18.  
  19. this.toString = () => JSON.stringify({
  20. name: this.getName(),
  21. firstName: this.firstName,
  22. lastName: this.lastName,
  23. moves: this.moves,
  24. })
  25. };
  26.  
  27. const cassieCage = new Character();
  28. [
  29. {
  30. name: 'Cassie'
  31. },
  32. {
  33. name: 'Cage'
  34. },
  35. {
  36. header: 'normals'
  37. },
  38. {
  39. Rollin: 'F1'
  40. }
  41. ].reduce((accumulator, current, index) => {
  42. if(index === 0) {
  43. cassieCage.setFirstName(current.name);
  44. }
  45. if(index === 1) {
  46. cassieCage.setLastName(current.name);
  47. }
  48. if(index > 2) {
  49. let [name] = Object.keys(current);
  50. cassieCage.addMove(name, current[name]);
  51. }
  52. console.log(accumulator)
  53.  
  54. return accumulator.concat(current);
  55. }, []);
  56.  
  57. console.log(cassieCage.toString()) // {"name":"Cassie Cage","firstName":"Cassie","lastName":"Cage","moves":[{"name":"Rollin","notatian":"F1"}]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement