Skip to content
Advertisement

Why my javascript looping return error missing ‘of ‘ after for

dataPoints: [
    for(i = 0;i<json.data.length;i++){
    { label: json.data[i][1], y: parseInt(json.data[i][2])}
    }]

I have code like that but I see an error on the console

SyntaxError: missing ‘of’ after for

Does anyone know why? I did search on Google, but nothing can tell me why.

Advertisement

Answer

Obviously, You will get incorrect syntax. Seems you want to create an array so use Array.map()

dataPoints: json.data.map(function (d) {
    return {
        label: d[1],
        y: parseInt(d[2])
    }
})
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement