0

I have an array of objects (student name, achievement, etc..) and I want to add any object that has the name "Jody" (for example) in a new empty array I created. However, when I try to print the name only of the new array of objects I get the undefined. Note: I can't use let, const etc..

Any ideas what I'm missing here?

var student;
var newStudents = [];
var students = [{
    name: 'Odelia',
    track: 'Accounting',
    achievements: '26',
    points: '2260'
}, {
    name: 'Jody',
    track: 'Web Design',
    achievements: '12',
    points: '890'
}, {
    name: 'Yann',
    track: 'Javascript',
    achievements: '10',
    points: '2266'
}, {
    name: 'Max',
    track: 'Marketing',
    achievements: '13',
    points: '1010'
}, {
    name: 'Jody',
    track: 'iOS',
    achievements: '9',
    points: '1002'
}, ]

for (var i = 0; i < students.length; i++) {
    student = students[i];
    if (student.name === 'Jody') {
        newStudents.push(student);
    }
}

console.log(newStudent.name);

output:

    0: 
      achievements: "12"
      name: "Jody"
      points: "890"
      track: "Web Design"
      __proto__: Object
    1: 
      achievements: "9"
      name: "Jody"
      points: "1002"
      track: "iOS"
      __proto__: Object
      length: 2
      __proto__: Array(0)

    undefined
4
  • 2
    Yep - that code works fine. Commented Apr 24, 2018 at 9:21
  • 1
    Should be console.log(newStudent[0].name); Because newStudent is a array Commented Apr 24, 2018 at 9:21
  • Is console.log(newStudent.name); a typo? The array is called newStudents, plural. If that's what you meant, then it's undefined because arrays don't have a name property. Commented Apr 24, 2018 at 9:23
  • yes guys sorry a typo from newStudent to newStudents with an s Commented Apr 24, 2018 at 9:29

4 Answers 4

4

should be newStudentsnot newStudent

    console.log(newStudents[0].name);

Also,newStudents is Array.

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

Comments

0

You are trying to print name outside of the loop. Cross check it again.

It should be like this:

for (var i = 0; i < students.length; i++) {
    student = students[i];
    console.log(newStudent.name);
    if (student.name === 'Jody') {
        newStudents.push(student);
    }
}

Comments

0

@Yann Bohbot,

There are 2 issues, 1. Put correct object name i.e. "newStudents" and NOT "newStudent" in console.log(); 2. You need to change below line to get values, console.log(newStudents[1].name);

Your are using multidimensional array and need iterate in proper way... :)

Comments

0

Try this, it's work for me;

const students    = [
    {
        name:         'Odelia',
        track:        'Accounting',
        achievements: '26',
        points:       '2260',
    },
    {
        name:         'Jody',
        track:        'Web Design',
        achievements: '12',
        points:       '890',
    },
    {
        name:         'Yann',
        track:        'Javascript',
        achievements: '10',
        points:       '2266',
    },
    {
        name:         'Max',
        track:        'Marketing',
        achievements: '13',
        points:       '1010',
    },
    {
        name:         'Jody',
        track:        'iOS',
        achievements: '9',
        points:       '1002',
    },
];

const newStudents = [];
for ( const student of students ) {
    if ( student.name === 'Jody' ) {
        newStudents.push( student );
    }
}

Or you can use filter :

const newStudents = students.filter( student => student.name === 'Jody' );

Comments

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.