Jump to content

Associative Array for JSON Stringifier


powerpants

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)) {
        ....
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.