0

I have some more complicated strings than as follow but to put it simple, suppose I have

var stats_members = ",\{\"y\"\: 6\}";
var data = 
    [{
                "x": "2012-11-05",
                "y": 6
            },+stats_members+
            ]
          ;

When I console.log(data)I keep on getting Uncaught SyntaxError: Unexpected token ] Could someone explain why ?

4
  • Your goal isn't obvious, here. So it's hard to fix your code. Commented Sep 20, 2013 at 9:04
  • why do you have two + (addition, concatenation) symbol with only one operand in your array declaration ? Commented Sep 20, 2013 at 9:04
  • Perhaps you would like to eval that string into an object and then just put a reference to it in your new object, with no plus signs. Commented Sep 20, 2013 at 9:05
  • @Lepidosteus because, here in fact I have some smarty dyanmically generated code. But to make it simple I put some strings to test Commented Sep 20, 2013 at 9:06

4 Answers 4

1

As the comments show what you are trying to achieve is unclear.

Also:

},+stats_members+

should be

},+stats_members
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works ! In fact I have some smarty dyanmically generated code. But to make it simple I put some strings to test
1

There is an extra '+' in the data variable declaration.

it should be:

var data = [{"x": "2012-11-05", "y": 6 }, stats_members];

Comments

1
var stats_members = ",\{\"y\"\: 6\}";
var data = 
    [{
                "x": "2012-11-05",
                "y": 6
            },+stats_members
            ]
          ;

That extra plus after "stats_members" is causing problem bro

Comments

0

You're doing some very weird stuff there. To make it very very simple, this is how the parser interprets your code:

var data = 
[                         //-> here starts an array literal
{                         //-> here starts an object literal (first array item)
  "x": "2012-11-05",      //-> here is the property x in the object literal
  "y": 6                  //-> here is the property y in the object literal
}                         //-> here ends the object literal
,                         //-> comma separating array items
+stats_members            //-> cast to number by the unary + sign
+                         //-> add something to the value on the previous row
]                         //-> array literal end should not be here, it is expecting something to add

If you want to explain exactly what you are trying to do, we could help with a better syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.