1

How to add a new property "comment" to all the students and return the ClassFoo?

ClassFoo = {
    "Total": 3, 
    "students": [
        {
            "name": "Peter",
            "grade": "C"
        },
        {
            "name": "Ben",
            "grade": "B"
        },
        {
            "name": "Ann",
            "grade": "B"
        },
    ]
};

Comments(B) = {
    "grade": "B",
    "comment": "Good"
};

Comments(C) = {
    "grade": "C",
    "comment": "Work harder"
};

Become like this

ClassFoo = {
    "Total": 3, 
    "students": [
        {
            "name": "Peter",
            "grade": "C",
            "comment": "Work harder"
        },
        {
            "name": "Ben",
            "grade": "B",
            "comment": "Good"
        },
        {
            "name": "Ann",
            "grade": "B",
            "comment": "Good"
        },
    ]
};

Should I create a new object? use .map for ClassFoo.students? and then match comments if Comments.grade === ClassFoo.students.grade, then .push comment?

4
  • so loop over it and add it... Commented Nov 6, 2018 at 20:37
  • 1
    Your syntax is not valid on line 2. Looks like ClassFoo it should be an {} object instead of an [] array. Commented Nov 6, 2018 at 20:37
  • do you want to get a new object or the same with mutated properties? why does Comments contain grade? Commented Nov 6, 2018 at 20:45
  • 'Commonts.grade' is to check the grade of each student Commented Nov 6, 2018 at 22:28

2 Answers 2

3

class ClassFoo {
  constructor(students) {
    this.total = students.length
    this.students = students
    this.students.forEach(student => {
      if (this.Comments[student.grade]) {
        student.comment = this.Comments[student.grade]
      }
    });
  }
  
  get Comments() {
    return {
      B: 'Good',
      C: 'Work Harder',
    }
  }
}

const classFoo = new ClassFoo([
  {
    "name": "Peter",
    "grade": "C"
  },
  {
    "name": "Ben",
    "grade": "B"
  },
  {
    "name": "Ann",
    "grade": "A"
  },
]);

console.log(classFoo)

Sign up to request clarification or add additional context in comments.

6 Comments

What if I don't know what are the comments for different grade yet?
Well an E or an F in this example would result in an undefined comment, which is relatively representative of the grade if it doesn't have a comment yet. If you would prefer to leave out the comment if it is undefined, that can be done.
Can it do Comments.grade === ClassFoo.students.grade ?
I don't want to list out all the comments for grades
In that case, simply check if (this.Comments[student.grade]) is a truthy value before adding it to the student – I've updated that answer.
|
0

You can use a for..of loop like this

ClassFoo = {
  "Total": 3,
  "students": [{
      "name": "Peter",
      "grade": "C"
    },
    {
      "name": "Ben",
      "grade": "B"
    },
    {
      "name": "Ann",
      "grade": "B"
    },
  ]
};

for (let student of ClassFoo.students) {

  if (student.grade === 'B') {
    student.comment = 'good';
  } else if (student.grade === 'C') {
    student.comment = 'work harder';
  }

}

console.log(ClassFoo)

A for..of loop will iterate over every element in the students array and add a property comment with the value of 'good' on a student object.

3 Comments

what if I don't know what are the comment for grade? and students have different grades?
Updated the answer, you can just put an if statement in your for of loop and in this way dynamically add comments based on the grade.
if I have 24 grades, it is bad to list out all the conditions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.