Skip to content

Instantly share code, notes, and snippets.

@iampeterbanjo
Created August 12, 2019 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iampeterbanjo/ef197544106655e806d870aa46ca2870 to your computer and use it in GitHub Desktop.
Save iampeterbanjo/ef197544106655e806d870aa46ca2870 to your computer and use it in GitHub Desktop.
let character = {};
updateCharacter = (path, value) => {
character[path] = value;
}
const Character = function() {
this.moves = [];
this.firstName = ''
this.lastName = ''
this.getName = () => `${this.firstName} ${this.lastName}`;
this.setFirstName = name => this.firstName = name;
this.setLastName = name => this.lastName = name;
this.addMove = (name, notatian) => {
this.moves.push({ name, notatian });
}
this.toString = () => JSON.stringify({
name: this.getName(),
firstName: this.firstName,
lastName: this.lastName,
moves: this.moves,
})
};
const cassieCage = new Character();
[
{
name: 'Cassie'
},
{
name: 'Cage'
},
{
header: 'normals'
},
{
Rollin: 'F1'
}
].reduce((accumulator, current, index) => {
if(index === 0) {
cassieCage.setFirstName(current.name);
}
if(index === 1) {
cassieCage.setLastName(current.name);
}
if(index > 2) {
let [name] = Object.keys(current);
cassieCage.addMove(name, current[name]);
}
console.log(accumulator)
return accumulator.concat(current);
}, []);
console.log(cassieCage.toString()) // {"name":"Cassie Cage","firstName":"Cassie","lastName":"Cage","moves":[{"name":"Rollin","notatian":"F1"}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment