javascriptjslint

Expected an identifier and instead saw '0'


How would this jslint error be fixed in here? https://jsfiddle.net/cfv2g1ho/

[JSLint was unable to finish] Expected an identifier and instead saw '0'.

function onYouTubeIframeAPIReady() {
    let playerVarsList = {
        0: {
            playerVars: {
                playlist: "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g"
            }
        },
        1: {
            playerVars: {
                listType: "playlist",
                list: "PLYeOyMz9C9kYmnPHfw5-ItOxYBiMG4amq"
            }
        }
    }
    //to add the player to all the play buttons
    const totalP = document.querySelectorAll('[data-container="play1"]').length;

    //looping over all the play buttons and adding player to that
    for (let i = 0; i < totalP; i++) {
        players.add(".playSingle" + i, (playerVarsList[i] || {}));
    }
    players.add(".playInitial", {});

}

Solution

  • 0 is not a valid identifier, "0" is however. But you're handling your object as an array - so why aren't you using an array instead of an object?

        let playerVarsList = [
            {
                playerVars: {
                    playlist: "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g"
                }
            },
            {
                playerVars: {
                    listType: "playlist",
                    list: "PLYeOyMz9C9kYmnPHfw5-ItOxYBiMG4amq"
                }
            }
        ]
    

    If you do have some reason to absolutely use an object, however:

        let playerVarsList = {
            "0": {
                playerVars: {
                    playlist: "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g"
                }
            },
            "1": {
                playerVars: {
                    listType: "playlist",
                    list: "PLYeOyMz9C9kYmnPHfw5-ItOxYBiMG4amq"
                }
            }
        }
    

    Be careful with using strict mode when mixing numbers and strings, e.g. playerVarsList[i] won't work if strict mode is enabled.