Having trouble accessing objects. They are printing as undefined. Help! I need the code to print the student names.
let students = [
{name: 'Remy', cohort: 'Jan'},
{name: 'Genevieve', cohort: 'March'},
{name: 'Chuck', cohort: 'Jan'},
{name: 'Osmund', cohort: 'June'},
{name: 'Nikki', cohort: 'June'},
{name: 'Boris', cohort: 'June'}
];
function objPrint() {
for (var i=0; i<students.length; i++) {
console.log("Name: " + students[i][0] + " Cohort: " + students[i][1])
}
}
You are accessing the items as if it is an array. But in reality is it an array of objects.
The top level, already gets the item that is the current item, just access the keys with dot or bracket notation to get the value.
let students = [ {name: 'Remy', cohort: 'Jan'}, {name: 'Genevieve', cohort: 'March'}, {name: 'Chuck', cohort: 'Jan'}, {name: 'Osmund', cohort: 'June'}, {name: 'Nikki', cohort: 'June'}, {name: 'Boris', cohort: 'June'} ]; students.forEach((item) => { //console.log(`Name - ${item.name} :: Cohort - ${item.cohort}`); console.log('Name - ' + item.name + " :: Cohort - " + item.cohort ); });
Do something like this:
let students = [
{name: 'Remy', cohort: 'Jan'},
{name: 'Genevieve', cohort: 'March'},
{name: 'Chuck', cohort: 'Jan'},
{name: 'Osmund', cohort: 'June'},
{name: 'Nikki', cohort: 'June'},
{name: 'Boris', cohort: 'June'}
];
function objPrint() {
for (var i=0; i<students.length; i++) {
// Can also use students[i]['name'] , students[i]['cohort']
// using lodash.js _.get(students, [i, 'name'], 'default value');
// using new destructuring let {name, cohort} = students[i] then console.log("name: "+ name + " Cohort: "+cohort);
console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort);
}
}
You need to call the key/attribute like this : students[i].name
and then the method objPrint()
to print the values.
let students = [ {name: 'Remy', cohort: 'Jan'}, {name: 'Genevieve', cohort: 'March'}, {name: 'Chuck', cohort: 'Jan'}, {name: 'Osmund', cohort: 'June'}, {name: 'Nikki', cohort: 'June'}, {name: 'Boris', cohort: 'June'} ]; function objPrint() { for (var i=0; i<students.length; i++) { console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort) } } objPrint();
Properties of objects are access by the dot notation rather than a bracket with number. So you should do it like this
students[i].name
Try for..of:
let students = [ {name: 'Remy', cohort: 'Jan'}, {name: 'Genevieve', cohort: 'March'}, {name: 'Chuck', cohort: 'Jan'}, {name: 'Osmund', cohort: 'June'}, {name: 'Nikki', cohort: 'June'}, {name: 'Boris', cohort: 'June'} ] // returns an object as a student for(let student of students) { console.log(`Name: ${student.name} Cohort: ${student.cohort}`) }
benefits of this approach
.foreach()
it works with break, continue, and return and also.. use backticks to avoid the old fashioned way of string concatenation - it looks better :)
Try this:
let students = [
{name: 'Remy', cohort: 'Jan'},
{name: 'Genevieve', cohort: 'March'},
{name: 'Chuck', cohort: 'Jan'},
{name: 'Osmund', cohort: 'June'},
{name: 'Nikki', cohort: 'June'},
{name: 'Boris', cohort: 'June'}
];
function sobjPrint() {
for (var i in students) {
if (students.hasOwnProperty(i)) {
console.log("Name: " + students[i].name + " Cohort: " + students[i].cohort)
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.