0

I have 2 javascript objects. I need to merge them together, however one of them contains dynamic form field values, saved to variables.

I have some example code here: http://jsfiddle.net/ZAa7L/

I got this code from another question on stackoverflow, and their example worked fine.

My problem is, whenever I run it, I get the javascript error: Uncaught SyntaxError: Unexpected token s

This is seriously infuriating me, as if I replace the variable name in the data1 object with a literal string, it works fine.

Any ideas how to get the startDay property to use a variable?

Many thanks

Phil

1 Answer 1

1

You can simply concatenate the string like this

var startDayVar = 1; var data1 = '{ "startDay" : ' + startDayVar + '}'; var data2 = '{ "applicationId": "events.save"}'; var json1 = JSON.parse(data1); var json2 = JSON.parse(data2); var obj = $.extend({},json1,json2); console.log(obj); 

FIDDLE

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

1 Comment

I cant believe I missed that out! Thank you very much. I'll accept the answer when I can

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.