Skip to content
Advertisement

I cant look for a value in a object array, it returns me error [closed]

Hello everyone I’m doing this music player and this is the song loader but the problem is that when I try to assing the value to song constant with lookSongbyId function it returns me an error idk why

let queue = [
    {
        id: 1,
        name: 'Crush',
        artist: 'Glades',
    }
]

const loadSong = (id) =>{


    function lookSongbyId(id)
    {
        queue.forEach(currentSong => {
            if(currentSong.id == id )
            {
                return currentSong
            }   
        })
    }

    const song = lookSongbyId(id)


    console.log(`la canciĆ³n ${song.name} ha sido cargada`)
}
loadSong(1)

song constant is undefined, and I dont know why aghhh If u could help me with this code I’ve been so thankful with u :DDD

Advertisement

Answer

Assuming the functionlookSongbyId is just misspelled (you must write function lookSongbyId), the forEach function cannot be used to return a value, as indirectly said here. Use a for ... of or .find() to retrieve the element

Advertisement