1

I've a code like this:

jQuery.each(images, function (k, v) {
    v = v.split(':');

    data.questions.push({
        source: questionAssetPath + v[0],
        match: v[1],
        mouse: mouseAssetPath + v[2] + '.png'
    });

    data.answers.push({
        answer: v[1],
        match: v[1]
    });
});

When I run a grunt task (browserify:js), I've got this error:

ParseError: Unexpected token +

Please help me figure out of this.

Thanks you.

UPDATE: I've solved it myself. Thanks guys. I've noticed that I can't use + operator with objects (wtf with me)

images = [
    "maria-ozawa.png:MariaOzawa:1",
    "aoi-125239.png:Aoi:2",
    "sasha-grey.png:SashaGrey:3"
];

jQuery.each(images, function (k, v) {
    v = v.split(':');
    v[0] = questionAssetPath + v[0];
    v[2] = mouseAssetPath + v[2] + '.png';
    data.questions.push({source: v[0], match: v[1], mouse: v[2]});
    data.answers.push({answer: v[1], match: v[1]});
});
6
  • 2
    Please post some sample data as well Commented Aug 25, 2014 at 8:42
  • Why not concatenate it in the variable before you build the JSON? Commented Aug 25, 2014 at 8:42
  • @Exception Why would you do that? Commented Aug 25, 2014 at 8:44
  • Seems that you have character + in your JSON, so provide JSON example where this error is happened. Commented Aug 25, 2014 at 8:44
  • 1
    I don't see any problem with this code. Maybe the problem comes from your data. Commented Aug 25, 2014 at 8:45

2 Answers 2

1
 Query.each(images, function (k, v) {
        v = v.split(':');
        questionAssetPath = questionAssetPath.concat(v[0])
        data.questions.push({source: questionAssetPath, match: v[1], mouse: mouseAssetPath + v[2] + '.png'});
        data.answers.push({answer: v[1], match: v[1]});
    });

That will get rid of the need of using '+' and will solve the error

See the JavaScript String concat() Method

Sign up to request clarification or add additional context in comments.

Comments

0

Check your data because this seems to work fine:

JSFIDDLE

var images = ["a:b:c","d:e:f"];
var data = {
    questions:[], 
    answers:[]   
};
var questionAssetPath = "c:\\abc\\";
var mouseAssetPath = "c:\\def\\";

$.each(images, function (k, v) {
    v = v.split(':');
    data.questions.push({source: questionAssetPath + v[0], match: v[1], mouse: mouseAssetPath + v[2] + '.png'});
    data.answers.push({answer: v[1], match: v[1]});
});

console.log(data);

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.