1

I'm new & doing this exercise on exercism.io, I'm trying to push the value pair into the object, here my code so far:

    class School{
    constructor(){
      this.rosterList = {};
    }
    roster(){
        return this.rosterList;
    }
    add(name,grade){
        let nameArry = [];
        this.rosterList.grade = nameArry.push(name);
    }
}

let a = new School;
a.add('Aimee', 2)
console.log(a.roster());

which result in

{ grade: 1 }

the result I tried to get

{ 2: [ 'Aimee' ] }

My questions are why the array become 1? How to push the name into a array like it should ? And how to push the "2" inside, not the "grade" , thank you

  • 1
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Teemu Jun 10 '18 at 08:40

4 Answers4

2

Your are getting output {grade : 1}, because in line:

this.rosterList.grade = nameArry.push(name);

You are simply assigning the prop grade the return value of nameArry.push(name), push method returns length of the array, which in your case is 1.

To get the desired result, change this line to :

 nameArry.push(name);
 this.rosterList[grade] = nameArry; 
amrender singh
  • 7,949
  • 3
  • 22
  • 28
  • With `this.rosterList[grade] = nameArry;` you will overwrite the existing value when adding a second name. – Asons Jun 10 '18 at 12:26
2

You need to check if the key exist, and then either push or assign the value, like this, where you use the bracket notation, this.rosterList[grade], to set the key.

Note, when assign with Arry.push(value) you will get the length of the array, not the array itself.

Stack snippet

class School{
    constructor(){
      this.rosterList = {};
    }
    roster(){
        return this.rosterList;
    }
    add(name,grade){
        this.rosterList[grade] ?
          this.rosterList[grade].push(name) :
          this.rosterList[grade] = [name];
    }
}

let a = new School;
a.add('tom', 2)
a.add('amy', 2)
console.log(a.roster());

Instead of using the ternary operator, you can use concat

add(name,grade){
    this.rosterList[grade] = (this.rosterList[grade]||[]).concat(name);           
}
Asons
  • 84,923
  • 12
  • 110
  • 165
1

You can use this.rosterList[grade].concat([name]) to append to add new names to already existing array for a particular grade. If the array for that grade doesn't exist then you can fist initialize this.rosterList[grade] = []. See the working code below:

 class School{
    constructor(){
      this.rosterList = {};
    }
    roster(){
        return this.rosterList;
    }
    add(name,grade){
        if(!this.rosterList[grade]) this.rosterList[grade]=[];
        this.rosterList[grade] = this.rosterList[grade].concat([name]);
    }
}

let a = new School;
a.add('tom', 2);
a.add('Aimee', 2);
console.log(a.roster());
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
0

array.push() returns thr new length of the array, so you can't do it in one line. But you can add the value directly as you create the array. And to use a variable as key you have to use the [] notation.

class School {
  constructor() {
    this.rosterList = {};
  }
  roster() {
    return this.rosterList;
  }
  add(name, grade) {
    let nameArry = [name];
    this.rosterList[grade] = nameArry;
  }
}

let a = new School();
a.add('tom', 2)
console.log(a.roster());
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
  • With `let nameArry = [name]; this.rosterList[grade] = nameArry;` you will overwrite the existing value when adding a second name. – Asons Jun 10 '18 at 12:25