Array Exercise Find (Function)

Hi, I am unable to run second code block of the find function.

let learningthisWeek = [{
name: ‘ayub’,
code: ‘javascript’,

}, {
learningThrough: ‘Mosh’,
learningPeriod: ‘4 weeks’,
dedicatedHours: ‘12 hours’

}, ];
const learning = learningthisWeek.find(function(learning) {

return learning.name === 'Mosh';

});
console.log(learning);

Reformatting that:

let learningthisWeek = [
  {
    name: 'ayub',
    code: 'javascript',
  }, {
    learningThrough: 'Mosh',
    learningPeriod: '4 weeks',
    dedicatedHours: '12 hours',
  }, 
];
const learning = learningthisWeek.find(
  function(learning) {
    return learning.name === 'Mosh';
  });
console.log(learning);

I am not exactly sure what you were expecting this to do, but it prints out undefined for me. Why? Because there is no object inside learningThisWeek that has the name field set to 'Mosh' (there is an object with the learningThrough field set to 'Mosh', but there is only one object with a name field and it is set to 'ayub'.

1 Like

In your code, you set learningthisWeek array name value as ayub:

             
const learning = learningthisWeek.find(function(learning) {

                learning.name === 'Mosh' // Undefine
                learning.learningThrough === 'Mosh' // will return your data
                learning.name ===  'auyb' // will return your data
});