I'm trying to asynchronously send some data as a single object. Half of the data is coming from my KnockoutJS viewModel. The other half is some data that I want to add on to that.
My thought was to convert them both to JSON objects, then use an array .concat to put them together. But this isn't working. Might you know why?
I've tried a few solutions. The first method builds an object from a JSON string, and then uses JSON.parse to put them as an object. The second tries to avoid strings altogether. Either way, after I get my objects I try concatenating them together, but without any luck.
With Strings
toAddString = '{"file": "thefile"}';
toAddObj = JSON.parse(toAddString);
koString = ko.toJSON(viewModel);
koObj = JSON.parse(koString,null,2);
finalObj = koObj.concat(toAddObj);
With Objects
toAddObj = [{"file": "thefile"}];
koObj = ko.toJS(viewModel);
finalObj = koObj.concat(toAddObj);
With Objects (2)
toAddObj = new Object();
toAddObj.file = "one";
koObj = ko.toJS(viewModel);
finalObj = koObj.concat(toAddObj);
Do you know what might be going wrong here?
All I want is a single object, be it an array or a JSON object, that contains the data from each of these sources.
ko.toJS(viewModel)give you as a result? Is it an array, or an object? Are you trying to end up with a single object with the combined properties oftoAddObjandkoObj? Or did you want an array?