Re: Storing Objects in an Associative Array (object)

Wednesday, October 17, 2012

Man your problem is very simple. You just need to move your line:
"var objClass        = new clsNewClass();"

To Inside your for. Exactly like that:

var arrObjectList        = new Object();

nodes = inXML.getElementsByTagName('thing');

for (ctr=0; ctr < nodes.length; ctr++) {
    var objClass        = new clsNewClass();        // <--- look here
    ....
    ....
}

Now after giving you the solution i ll try to explain what happened.
The problem is that you need to instantiate a new object for every node otherwise you would be replacing the attribute values of your only object and adding the same object many times into your array.

So i hope it solve your problem, but if it doesnt let me now and ill try to help you again.

Good luck!
Miguel Almeida

2012/10/17 Binary Tutor <jlarsonfour3@gmail.com>
See ***JAL below.
James Larson
In God We Trust...


On Friday, September 14, 2012 4:40:26 PM UTC-4, FreeXenon wrote:
OK, I am getting my ass kicked by this problem. I am storing class instantiated objects into an associative array and one of two things is happening after adding the objects to the associative array 
only the last object added is available, or
all of the associated indices have the same value as the last object added. 
I have verified that the data that is coming in from the XML file and being pushed into into the object just prior to adding it to the associative array and it is fine, but after adding it to the associative array (declared using new Object() )... it is all messed up. 

Here is an example of the code I am working with:


Associative Array 

var arrObjList = new Object();

arrObjList = ProcessObjectList();     // see function declaration below


Adding the Class Objects to the Associative Array from above

ProcessObjectList = function (inXML)
{
var arrObjectList        = new Object();
var objClass        = new clsNewClass();        // see class declaration below
nodes = inXML.getElementsByTagName('thing');
for (ctr=0; ctr < nodes.length; ctr++)
{
// ***JAL seName is misspelled. should be setName according to declaration provided below.
objClass.seName    ( nodes[ctr].getAttributeNode('name').nodeValue );
objClass.setValue1 ( nodes[ctr].getAttributeNode('value1').nodeValue );
objClass.setValue2 ( nodes[ctr].getAttributeNode('value2').nodeValue );

arrObjectList[ objClass.geName() ] = objClass;
} /* end for loop */
return arrObjectList;
} /* end Process Object List */


Class Code

function clsNewClass()
{
  var Name = '';
var Value1 = '';
var Value2 = '';

this.geName = function()        { return Name; }
this.getValue1 = function()        { return Value1; }
this.getValue2 = function()        { return Value2; }

this.setName = function(inVal) { Name = inVal; }
this.setValue1 = function(inVal) { Value1 = inVal; }
this.setValue2 = function(inVal) { Value2 = inVal; }
      } // end class New Class


So what do you think. I have a lot of coding that is stalled by this. I do not code for a living, and at some point I will pull out jquery or prototypejs and convert things over, but that is not what I am looking for right now. I tried this by declaring the may associative array in the following ways, none of which worked: new Array(), new Object(), and Object();

Thanks guys.

--
 
 

--
 
 

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright © 2010 Web Design | Free Blogger Templates by Splashy Templates | Layout by Atomic Website Templates

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Animals Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos Matilde futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii