3 Replies - 675 Views - Last Post: 27 February 2013 - 08:11 PM

#1 g37752   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

how do i declare an associative array with an array as the value?

Posted 27 February 2013 - 07:45 PM

for a simple associative array:

var assarray={};
assarray[key]=value;

or
var assarray={'key1':'val1','key2':'val2',...}


how can i declare an associative array, so i can for example,

assarray[key].push('something');


i.e., i want the value to be an array.

This post has been edited by g37752: 27 February 2013 - 07:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how do i declare an associative array with an array as the value?

#2 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: how do i declare an associative array with an array as the value?

Posted 27 February 2013 - 07:54 PM

Javascript doesn't have associative arrays, it has objects. {} is an empty object, not an associative array.

That aside, .push() is an array function. It can only operate on an array. It pushes an element onto the array, or in other words, places a new value onto the end of an array. It is important to understand this, for two reasons:
  • It is pushing a value - arrays store values, objects store key-value relationships
  • Objects don't have a beginning and an end, and operations like push() and unshift() don't make sense


Why do you want to push using an object? Surely it's acceptable to do this:
assarray[key] = 'something';


This post has been edited by e_i_pi: 27 February 2013 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 g37752   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: how do i declare an associative array with an array as the value?

Posted 27 February 2013 - 08:00 PM

yes, i know i was declaring an object.

assarray[key] = 'something';
would not work, because i want it to be an array. this line would wipe out previous entries.

so assarray should look something like:

{'key1':array1,'key2':array2,....}
so how do i declare or initialize such an object?
so i can do
assarray[key].push('something');

This post has been edited by g37752: 27 February 2013 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: how do i declare an associative array with an array as the value?

Posted 27 February 2013 - 08:11 PM

So initial declaration of the object would be like this?
var myObject = {
  'key1': [],
  'key2': []
}


If so, then you can push an item onto that key1 array like this:
myObject['key1'].push('something');



If, on the other hand, you are trying to add another property to the object, you can't use push as push only supplies a value, whereas a new property requires a key and a value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1