• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

adding elements to hash array

Hi,

I have a javascript function which has a hash with arrays written like this:

var array_hash = {
      "one": "Valuesarrayforone",
      "two": "Valuesarraytwo",
      "three": "Valuesarraythree"
};

"valuesarrayforone is declared as an array like this:

var Valuesarrayforone = new Array(
new Array ("Technologies1", "Technologies1", ""),
new Array ("Technologies2", "Technologies2", ""),
new Array ("Technologies3", "Technologies3", ""),
new Array ("Technologies4", "Technologies4", ""),
new Array ("Technologies5", "Technologies5", ""),
);


Now I need to add one more element like
 "four": "Valuesarrayfour"

dynamically to array_hash .

How do I do that? Please let me know which function do I use..

Thanks
0
perfectsujatha
Asked:
perfectsujatha
  • 3
  • 3
1 Solution
 
dakydCommented:
You can do it without a function:
  array_hash["four"] = "Valuesarrayfour";

Basically, the notation you have is shorthand, and the syntax that I have is how to add a single element at a time.  Here's a sample script that shows how it works.  Hope it helps.

<script type="text/javascript">
var array_hash = {
     "one": "Valuesarrayforone",
     "two": "Valuesarraytwo",
     "three": "Valuesarraythree"
};

array_hash["four"] = "Valuesarrayfour";

// now alert it back out so you can see that it changed
var str = "";
for (var i in array_hash)
  str += i + ": " + array_hash[i] + "\n";

alert(str);
</script>
0
 
perfectsujathaAuthor Commented:
can I do like this from other function?

Select.setParams = function (displayField, selectType, defaultValue) {
                array_hash["four"] = selectType;
      displayField.selectType = selectType;
      displayField.defaultValue = defaultValue;
};


I will be calling this "Select.setParams" from my html or jsp file passing 3 parameters. The "selectType" would be the one we add dynamically to the hash array.
0
 
dakydCommented:
Are you trying to append to the hash each time you call Select.setParams?  That's not what will happen with the function, as is.  The way you have it now, your function will always set the key as "four" and the value as selectType.  I suspect that's not what you want, so you'd have to make sure that the key changes.  I can provide some ideas for how to do that, but first ...

If you *are* trying to append to the end, why use the hash in the first place?  A regular array (w/ numerical indeces) would do this fairly easily, and your keys are string representations of numbers anway, so why do you need the hash?  If you already know that you want array_hash["one"], then why couldn't you look up array[1]?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
perfectsujathaAuthor Commented:
Ok may I didn't explain properly.

I have an existing "arry_hash" with three keys and corresponding arrays. When ever I call "Select.setParams" this should append (at the end) "selectType" as fourth key and the corresponding array also will be "selectType" only.

After calling "Select.setParams", if my variable value of selectType is "four", my hash should look like

var array_hash = {
     "one": "Valuesarrayforone",
     "two": "Valuesarraytwo",
     "three": "Valuesarraythree",
     "four":  "four"
};

Is this possible? I don't want the fourth one to be physically there, but I will be accessing the dynamically added fourth key and fourth array to render to populate a drop down.

0
 
dakydCommented:
It's possible, and what you posted above came pretty close.  This should do it:

Select.setParams = function (displayField, selectType, defaultValue) {
     array_hash[selectType] = selectType;
     displayField.selectType = selectType;
     displayField.defaultValue = defaultValue;
};

I'm assuming that selectType is a string, since you'll need your key to be a string.

The reason for my shpiel above was that the function you had before would always write to the same key, "fourth", so you'd be constantly overwriting the value in your hash if you called Select.setParams multiple times.  If you're varying the key by using selectType as the key, then that should solve your problem.

If you have any more questions, ask away.  Hope that helps.
0
 
perfectsujathaAuthor Commented:
thanks!! I got through!!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now