Alfonso Alfonso - 2 months ago 9
Javascript Question

Unexpected token square bracket in Nodejs

Here is a simple example:

for (i = 1; i < totalRows; i++) {
if(rows[i][10] == null) {
spreadsheet.add({ [i]: { 10: "Added" } });
}
}


why do I get an error "Unexpected token [" here:

spreadsheet.add({ [i] : { 10:
^


I am trying to run the app on
nodejs
version
v0.10.35
,
I cant update it as its a free server provided by OpenShift.
It runs fine on my
localhost
computer with
nodejs v4.4.7


Please advice a workaround for this code, so it could be executed.

Basically what this code does is, it looks in every row if
column 10 is empty
, and if its empty, it adds Added

Answer

You defining an object property invalidly.

This is the code that will work:

for (i = 1; i < totalRows; i++) {
    if(rows[i][10] == null)  {
      var obj = {};
      obj[i] = { 10: "Added" };
      spreadsheet.add(obj);
    }
}

Check MDN documentation regarding the working with properties: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects