powerpants Posted March 2, 2009 Share Posted March 2, 2009 Hi, I'm attempting to create an associative array in Javascript for a bunch of images that my page is displaying. I already have an associative array called currentlyDisplayedPicturesData which is keyed by [imageId]. I want to make a new associative array called editPicturesInfo keyed by imageId's from currentlyDisplayedPicturesData and make a new associative array called editPicturesInfo, with each imageId associated with some information taken from forms in the document. var editPicturesInfo = new Object(); for(imageId in currentlyDisplayedPicturesData) { editPicturesInfo[imageId]["selectionStatus"] = currentlyDisplayedPicturesData[imageId]["selectionStatus"]; editPicturesInfo[imageId]["caption"] = document.getElementById("caption" + imageId).value; editPicturesInfo[imageId]["deleteStatus"] = document.editPicturesForm.deleteBox.value; editPicturesInfo[imageId]["indexStatus"] = document.editPicturesForm.indexNow.value; editPicturesInfo[imageId]["peopleToAdd"] = document.editPicturesForm.people.value; } Firebug keeps telling me that editPicturesInfo[imageId] is not defined! Shouldn't it be automatically defined as I am making the above statements? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 2, 2009 Share Posted March 2, 2009 Error: Implied global: currentlyDisplayedPicturesData 3 5, document 6 7 8 9, imageId 5 6 7 8 9 Problem at line 1 character 28: Use the object literal notation {}. var editPicturesInfo = new Object(); Problem at line 5 character 36: ['selectionStatus'] is better written in dot notation. editPicturesInfo[imageId]["selectionStatus"] = currentlyDisplayedPic... Problem at line 5 character 97: ['selectionStatus'] is better written in dot notation. editPicturesInfo[imageId]["selectionStatus"] = currentlyDisplayedPic... Problem at line 6 character 36: ['caption'] is better written in dot notation. editPicturesInfo[imageId]["caption"] = document.getElementById("capt... Problem at line 7 character 36: ['deleteStatus'] is better written in dot notation. editPicturesInfo[imageId]["deleteStatus"] = document.editPicturesFor... Problem at line 8 character 36: ['indexStatus'] is better written in dot notation. editPicturesInfo[imageId]["indexStatus"] = document.editPicturesForm... Problem at line 9 character 36: ['peopleToAdd'] is better written in dot notation. editPicturesInfo[imageId]["peopleToAdd"] = document.editPicturesForm... Problem at line 3 character 7: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype. for(imageId in currentlyDisplayedPicturesData) Quote Link to comment Share on other sites More sharing options...
powerpants Posted March 2, 2009 Author Share Posted March 2, 2009 Can you please explain to me what this means? I'm still learning Javascript. Sorry.... Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 2, 2009 Share Posted March 2, 2009 Can you please explain to me what this means? I'm still learning Javascript. Sorry.... Dot-notation is most commonly used in JS to access properties of objects, which means that the property name is given after a full stop character. For example: Given: <form id="myForm" action="/"> <div><input name="foo"></div> </form> We can say: var myForm = document.getElementById("myForm"); var input = myForm.foo; Quote Link to comment Share on other sites More sharing options...
powerpants Posted March 2, 2009 Author Share Posted March 2, 2009 Ok, I tried it using full stops, but I am still getting the error editPicturesInfo.imageId is not defined! Don't these statements define it? var editPicturesInfo = {}; for(imageId in currentlyDisplayedPicturesData) { editPicturesInfo.imageId.selectionStatus = "selected"; //This is the line that Firebug catches. editPicturesInfo.imageId.caption = "caption"; editPicturesInfo.imageId.deleteStatus = "true"; editPicturesInfo.imageId.indexStatus = "true"; editPicturesInfo.imageId.peopleToAdd = "2"; } Thanks for looking at this. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 2, 2009 Share Posted March 2, 2009 Problem at line 3 character 7: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype. The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the members which were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data members. The body of every for in statement should be wrapped in an if statement that does filtering. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype. For example, for (name in object) { if (object.hasOwnProperty(name)) { .... } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.