5

I have an array like this:

var myArray = new Array();

myArray['foo'] = {
    Obj: {
        key: value
    }
};
myArray['bar'] = {
    Obj: {
        key: value
    }
};

When I do console.log(myArray) I just get empty [ ]. And when I try to iterate the array using jQuery's each the function doesn't run.

How can I get the 'foo' and 'bar' parts of the array?

Example code:

console.log(myArray); // [ ]

jQuery.each(myArray, function(key, obj) {
    console.log(key); // should be 'foo' following by 'bar'
});

In addition, why does this work:

jQuery.each(myArray[foo], function(obj, values) {

    // Why does this work if there are no associative arrays in JS?

});
Abhinay Reddy Keesara
  • 9,763
  • 2
  • 18
  • 28
beingalex
  • 2,416
  • 4
  • 32
  • 71
  • 3
    There are no associative arrays in javascript, when using strings as keys, you have an object. – adeneo Jul 23 '14 at 11:11
  • So how come I can `jQuery.each(myArray[foo], function(key, obj) {` ? How can I use a sort of associated key array as I need to create an array of settings for a project – beingalex Jul 23 '14 at 11:12
  • this is multi dimensional array in this case it is 2 dimensional – Pramod S. Nikam Jul 23 '14 at 11:16

6 Answers6

3

you can get keys by:

Object.keys(variable name);

it returns array of keys.

Super Hornet
  • 2,839
  • 5
  • 27
  • 55
2

You need to define it as an object if you want to access it like that:

var myObj= {};

myObj.foo = ...;
myObj.bar = ...;

Now you can access the properties like myObj["bar"] or myObj.bar

Note:
To loop through all the properties it's wise to add an additional check. This is to prevent you from looping through inherited properties.

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        // Do stuff.
    }
}
Jonathan
  • 8,771
  • 4
  • 41
  • 78
  • Note: Arrays are actually objects too in Javascript with their own properties that you can set/get. This is why the last example in the question worked. – Ben Jul 23 '14 at 11:21
  • Will accept this answer. I never knew the browser converted the array to an object. – beingalex Jul 23 '14 at 11:21
2

Array is a collection where each element has an index. To add element to array you can use push method

 myArray.push('someValue');

or set element by index (if length of array < index):

 myArray.push('someValue1');
 myArray.push('someValue1');
 myArray[0] = 'new someValue1';

Note that array is an instance of Object class, so you can add/edit any property of this object:

myArray.foo = '1';
myArray['bar'] = '2';

In this case you will not add new element to array, you defining new properties of object. And you don't need to create object as Array if you don't wont to use indexes. To create new object use this code:

var myObj = {};

To get all properties of object see How to get all properties values of a Javascript Object (without knowing the keys)?

Community
  • 1
  • 1
Livon
  • 436
  • 1
  • 3
  • 10
0
var myArray = {};

myArray['foo'] = {  'key': 'value'  }

myArray['bar'] ={  'key': 'value'  }

console.log(myArray)

jQuery.each(myArray['foo'], function(obj, values) {

   console.log(obj, values)

});

Demo

0

With your Array of Objects you could use this function:

var getKeys = function(obj) {
  if (!(typeof obj == "object")) return [];
  var keys = [];
  for (var key in obj) if (obj != null && hasOwnProperty.call(obj, key)) keys.push(key);
  return keys;
};

getKeys(myArray) would give you an array of your Keys.

This is basically a cleared up version of underscores _.keys(myArray) function. You should consider using underscore.

DerZyklop
  • 3,672
  • 2
  • 19
  • 27
0

// $.each() function can be used to iterate over any collection, whether it is an object or an array.

var myArray = {};
myArray['alfa'] = 0;
myArray['beta'] = 1;
$.each(myArray, function(key, value) {
      alert(key);
});

Note: checkout http://api.jquery.com/jQuery.each/ for more information.

Abhinay Reddy Keesara
  • 9,763
  • 2
  • 18
  • 28