Creating Javascript Object From Array Values
I am attempting to create a javascript object so that I can tranfsorm it into a json string in order to update a mysql table via php. I'm new to objects and json in javascript and
Solution 1:
Looks like your recent edit fixed one of the issues: you need []
around your arrays, not {}
.
Next, you need to change your array variable names to match what you're using in your for loop.
Finally, myObject should probably be an array, not an object, so change its {}
to []
.
Here's an updated version of your code:
var idArray = [ 1, 2, 3];
var slideArray = [ 1, 2, 3];
var publishArray = [ 0, 1, 0];
var floatArray = [ 1, 0 , 1];
var myObject = [];
for(var i = 0; i < idArray.length; i++) {
myObject[i] = {
slideId : idArray[i],
slideNo : slideArray[i],
isPublished : publishArray[i],
floatText : floatArray[i]
};
}
alert(myObject[0].slideId);
Solution 2:
use JSON.stringify to convert object to json string
alert(JSON.stringify(myObject[i]));
Post a Comment for "Creating Javascript Object From Array Values"