1

I'm have trouble finding a way to push values into an array dynamically. I have given the following situation:

var get_anchors= new Array('pitzel','mitzel','sizzle') 

current_anchor= pics[key].anchor; //has the value 'sizzle'

get_anchors[current_anchor].push(new Array('sizzle2','sizzle3'))

Javascript fails and says get_anchors[current_anchor] is undefined

How can I make get_anchors[current_anchor] work. Or is there a different way to get this done?

The desired result should look like 'pitzel','mitzel','sizzle'['sizzle2','sizzle3]

Jurudocs
  • 8,595
  • 19
  • 64
  • 88
  • What exactly is this for? Arrays have numeric indexes, so of course `get_anchors[current_anchor]` is `undefined`, since `current_anchor` is not `0`,`1` or `2`. What are you trying to accomplish? You will get better answers if you provide more information. – Felix Kling May 08 '12 at 13:58
  • hi thanks for asking...in fact that is a part of a loop through a json object. i have some anchors very often with the same name and names of pictures associated with the anchor name. in the end i want to have an array[anchor] with anchors and its corresponding pictures...[anchorname][['picname1'],['picname2']] – Jurudocs May 08 '12 at 14:08
  • 1
    So `pitzel`, `pitzel` and `sizzle` are anchors and `sizzleX` are picture names? Seems like you want a hash map instead of an array... – Felix Kling May 08 '12 at 14:10

2 Answers2

4

Based on your comment it looks like you want a hash map instead of an array. You can use an object for this:

var anchors = {
    'pitzel': [],
    'mitzel': [],
    'sizzle': []
};

Then you can do:

anchors[current_anchor].push('sizzle2', 'sizzle3');

or assuming that anchors does not have a property with the value of current_anchor, simply assign a new array:

anchors[current_anchor] = ['fooX', 'fooY'];

Of course you can populate the object dynamically as well. Have a look at Working with Objects for more information.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • @unbeli yeah.. and in php this is pretty cool and works ;-P but in python that will work too...or not ?! – Jurudocs May 08 '12 at 14:17
  • 1
    @Jurudocs: It would not work in Python either. In PHP, arrays are powerful and can be used as both, numerical and associative arrays. That is not the case in other languages though. Python has dictionaries, JavaScript has objects, Java has HashMap, etc. – Felix Kling May 08 '12 at 14:19
  • In fact, Javascript arrays are objects too, can be (mis)used as dictionaries. – unbeli May 08 '12 at 14:30
  • @unbeli: Yes, but I think that is more confusing than helping for less experienced JS developers ;) – Felix Kling May 08 '12 at 14:31
  • @FelixKling you told me about the var anchors = { 'pitzel': [], 'mitzel': [], 'sizzle': [] }; Is it possible to generate the keys dynamically? – Jurudocs May 08 '12 at 20:23
  • @Jurudocs: Yes, have a look at the [documentation](https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Objects_and_properties) I linked to or [this question](http://stackoverflow.com/questions/1184123/is-it-possible-to-add-dynamically-named-properties-to-javascript-object). – Felix Kling May 08 '12 at 20:28
  • @FelixKling okay i think i might be on a good way ;-) I got confused of the array and object literal thing...Thanks für your help. Also for the good mozilla documentation link! – Jurudocs May 08 '12 at 20:41
  • @Jurudocs: `Thanks für your help.`, auch ein schoener Mix aus Englisch und Deutsch ;) :) Bitteschoen, you're welcome! Happy coding :) – Felix Kling May 08 '12 at 20:55
  • @FelixKling I'm totally through with the world ;-PPP Coding all day and night long... – Jurudocs May 08 '12 at 21:02
0

I'm not sure I understand what you're trying to do, but I think you're trying to insert a few elements after where another occurs. This will do the trick:

var get_anchors = [ 'pitzel', 'mitzel', 'sizzle' ];

current_anchor = get_anchors.indexOf(pics[key].anchor);
get_anchors.splice(current_anchor + 1, 0, 'sizzle2', 'sizzle3');

// get_anchors = [ 'pitzel', 'mitzel', 'sizzle', 'sizzle2', 'sizzle3' ]
LukeGT
  • 2,324
  • 1
  • 21
  • 20