reduce question

0

I have the following code but I do not see clearly why acc[student.name] and , {} of the end, with numbers I see it but I do not understand why [] to access the student when I thought it would be with . :

var students = [{
    name: "Tony Parker",
    firstProject: 80,
    secondProject: 75,
    finalExam: 90,
    age: 25,
    skills: ['C', 'Java', 'JavaScript']
  },
  {
    name: "Marc Barchini",
    firstProject: 84,
    secondProject: 65,
    finalExam: 65,
    age: 30,
    skills: ['Swift', 'JavaScript']
  },
  {
    name: "Claudia Lopez",
    firstProject: 45,
    secondProject: 95,
    finalExam: 99,
    age: 49,
    skills: ['Java', 'Pearl']
  },
  {
    name: "Alvaro Briattore",
    firstProject: 82,
    secondProject: 92,
    finalExam: 70,
    age: 24,
    skills: ['JavaScript', 'PHP']
  },
  {
    name: "Isabel Ortega",
    firstProject: 90,
    secondProject: 32,
    finalExam: 85,
    age: 21,
    skills: ['Lua', 'Python']
  },
  {
    name: "Francisco Martinez",
    firstProject: 90,
    secondProject: 55,
    finalExam: 78,
    age: 45,
    skills: ['C++', 'Python']
  },
  {
    name: "Jorge Carrillo",
    firstProject: 83,
    secondProject: 77,
    finalExam: 90,
    age: 32,
    skills: ['C#', 'C++', 'C']
  },
  {
    name: "Miguel López",
    firstProject: 80,
    secondProject: 75,
    finalExam: 75,
    age: 31,
    skills: ['Scala', 'Java']
  },
  {
    name: "Carolina Perez",
    firstProject: 85,
    secondProject: 72,
    finalExam: 67,
    age: 27,
    skills: ['Scala', 'Java', 'JavaScript']
  },
  {
    name: "Ruben Pardo",
    firstProject: 89,
    secondProject: 72,
    finalExam: 65,
    age: 28,
    skills: ['C#', 'JavaScript']
  }
]


var result = students.reduce(function(acc, student) {
  acc[student.name] = student.age;
  return acc;
}, {});
console.log(result);
    
asked by francisco dwq 07.05.2018 в 18:01
source

2 answers

0

In this case, student represents each of the objects in the original array. The array acc is the accumulator or result of the function. The final result uses as a key the name of each object and as a value the age. Therefore, to access the name of each object, student.name is used. The {} at the end are optional and is an initial value that can be reduced to the first iteration.

    
answered by 07.05.2018 в 18:09
0

The second parameter of Array#reduce represents the default value. That said, we know it will be an empty literal object.

var result = students.reduce(function(acc, student) {
  acc[student.name] = student.age;
  return acc;
}, {});

Now, in each iteration, this object is modified to add a new key which is the name of the student and as a value, its age.

As an extra, you can rewrite that function in the following way (ES6):

const students = [{
    name: "Tony Parker",
    firstProject: 80,
    secondProject: 75,
    finalExam: 90,
    age: 25,
    skills: ['C', 'Java', 'JavaScript']
  },
  {
    name: "Marc Barchini",
    firstProject: 84,
    secondProject: 65,
    finalExam: 65,
    age: 30,
    skills: ['Swift', 'JavaScript']
  },
  {
    name: "Claudia Lopez",
    firstProject: 45,
    secondProject: 95,
    finalExam: 99,
    age: 49,
    skills: ['Java', 'Pearl']
  },
  {
    name: "Alvaro Briattore",
    firstProject: 82,
    secondProject: 92,
    finalExam: 70,
    age: 24,
    skills: ['JavaScript', 'PHP']
  },
  {
    name: "Isabel Ortega",
    firstProject: 90,
    secondProject: 32,
    finalExam: 85,
    age: 21,
    skills: ['Lua', 'Python']
  },
  {
    name: "Francisco Martinez",
    firstProject: 90,
    secondProject: 55,
    finalExam: 78,
    age: 45,
    skills: ['C++', 'Python']
  },
  {
    name: "Jorge Carrillo",
    firstProject: 83,
    secondProject: 77,
    finalExam: 90,
    age: 32,
    skills: ['C#', 'C++', 'C']
  },
  {
    name: "Miguel López",
    firstProject: 80,
    secondProject: 75,
    finalExam: 75,
    age: 31,
    skills: ['Scala', 'Java']
  },
  {
    name: "Carolina Perez",
    firstProject: 85,
    secondProject: 72,
    finalExam: 67,
    age: 27,
    skills: ['Scala', 'Java', 'JavaScript']
  },
  {
    name: "Ruben Pardo",
    firstProject: 89,
    secondProject: 72,
    finalExam: 65,
    age: 28,
    skills: ['C#', 'JavaScript']
  },
];

const result = students.reduce((acc, student) => ({
  ...acc,
  [student.name]: student.age,
}), {});

console.log(result);
    
answered by 07.05.2018 в 18:44