The result is not displayed, only this appears in the console:
The students who passed are: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example:
const estudiantes =[
{nombre:'Angelica', nota: 9.2},
{nombre:'Felipe', nota: 9.0},
{nombre:'Katia', nota: 9.4},
{nombre:'Jose', nota: 8.8},
{nombre:'Daniel', nota:8.0},
{nombre:'Alberto', nota: 7.2},
{nombre:'Fernanda', nota: 7.0},
{nombre:'Carla', nota:6.4},
{nombre:'Jesús', nota: 6.8},
{nombre:'Daniel<', nota:5.0}
];
let aprobado = estudiantes.filter(estudiante => estudiante.nota >= 7.0);
console.log(`Los estudiantes que aprobaron son: ${aprobado}`);
You get that result because if you print
aprobado
, you get something like this:You get this:
Given which you need to go through it with a loop, for example the
foreach()
Your code should look like this:
Once you go through it, you are already in a position to access the keys of the different contained objects, in this case the one we want to obtain is the name , so we do it with the syntax of:
variable.clave
Important
The problem is the way you are printing on the console, when interpolating the variable using
${var}
JavaScript you are taking the explicit value of the variable and printing it on the console, that is, an "object of objects", you have 2 possible solutions for this:Send the variable to log as an additional parameter:
console.log('Students who passed are:', passed);
Serialize the content of the variable before printing it
console.log(
Los estudiantes que aprobaron son: ${JSON.stringify(aprobado)}
);It would be something like this:
The function
console.log
does not print an array of objects. You have to iterate the result and print it like this:The reason you're getting that response is because you're printing objects as strings and the conversion is to
[object Object]
.Within your template literal, you can use map to return a
Array
with the names of the students and generate a string using thejoin
using as separator method,
.Here is the example.
Another option you have, with
reduce
could be like this:The browser console can receive more than one parameter, I think the easiest way to do what you want is this way:
console.log('Los estudiantes que aprobaron son: ', aprobado);
that would give the following result.