-2

Possible Duplicate:
Length of Javascript Object (ie. Associative Array)
Loop through JavaScript object

var location = {
     "Steve": "New York",
     "Abigayle": "Chicago"
}

for (var i = 0; i < location .length; i++)
{
    console.log('works');
}

I'm trying to make an array, where each item has some name and value.

The code above doesn't work. Tryed to make an object, but it doesn't have a length property - no for loop.

location= {
     "Steve": "New York",
     "Abigayle": "Chicago"
};

Is it possible to use arrays in this context?

Community
  • 1
  • 1
Jasper
  • 5,090
  • 11
  • 34
  • 41

4 Answers4

2

If you just want to work with what you have,

var location = {
     "Steve" : "New York",
     "Abigayle" : "Chicago"
}

for (var name in location) {
    console.log( name, location[name] );
}

If you care about the length, use an Array of objects

var location = [
     { key : "Steve", value : "New York" },
     { key : "Abigayle", value : "Chicago" }
];

But there is no easy way to look it up, it would require a loop.

epascarello
  • 204,599
  • 20
  • 195
  • 236
1
var locations = [
     ["Steve","New York"]
    ,["Abigayle","Chicago"]
];

or

var locations = [
      {Name:"Steve",Location:"New York"}
     ,{Name:"Abigayle",Location:"Chicago"}
];

you could output the data in the 1st option like this:

var delimiter = "    ";
console.log("var locations = [");
for (var i=0; i<locations.length; i++)
{
    var innerdelimiter = "";
    var line = delimiter + "[";   
    for (var j=0; j<locations[i].length;j++)
    {
       line += innerdelimter + locations[i][j];
       innerdelimiter = ",";
    }
    line += "]";
    console.log(line);
    delimiter = "   ,";
}
console.log("];");

and data in the 2nd option like this:

var delimiter = "    ";
console.log("var locations = [");
for (var key in locations)
{
    console.log(delimiter + "{" + key + ":" + locations[key] + "}");
    delimiter = "   ,";
}
console.log("];");
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • what about `for`, how do I get `new york` from `steve`? – Jasper Dec 04 '12 at 20:40
  • 1
    Now that I've given you something that actually compiles, why don't you play with it a bit? – Joel Coehoorn Dec 04 '12 at 20:41
  • for the first var, seems I have to use [0] or [1], for the second its `.name` and `.location`. Maybe you know a better way to handle data inside loop. – Jasper Dec 04 '12 at 20:44
  • second var looks too complicated, I have to make many dublicates of the object item names. It doubles an array size. – Jasper Dec 04 '12 at 20:46
  • So tempted to turn this into a [Quine](http://en.wikipedia.org/wiki/Quine_(computing)), but I haven't the time right now. – Joel Coehoorn Dec 04 '12 at 20:52
1

Just for reference, you can iterate over all the keys in an object:

location = {
     "Steve": "New York",
     "Abigayle": "Chicago"
};

for (var elem in location) {
    console.log(elem);
}

Produces:

Steve
Abigayle

But I think that one of the other answers is probably the correct way to what you're looking for.

Xymostech
  • 9,710
  • 3
  • 34
  • 44
  • Looks good. I would like to operate with `name` and `value` inside loop, not just a `value`. Is it possible? – Jasper Dec 04 '12 at 20:42
  • Well, then you just need to do the lookup `location[elem]`. However, like I said, I don't think this is the way you should be doing this, in case you have duplicate names, etc. – Xymostech Dec 04 '12 at 20:43
  • A word of warning, there is no guaranteed sort order when looping an object like this so you may find edge cases that don't behave as expected. – JaredMcAteer Dec 04 '12 at 20:45
  • Dublicate names is not an issue for the context where it will be used. Thanks! – Jasper Dec 04 '12 at 20:53
1

You can loop over object keys aswell. Only if you require indexed keys you should use an Array here.

var loc = {
     "Steve": "New York",
     "Abigayle": "Chicago"
};

Object.keys( loc ).forEach(function( name ) {
    console.log('name: ', name, ' city: ', loc[ name ] );
});

By the way, location is a (pretty much) reserved variable name within the window object. You can't really overwrite that, so you should re-name that variable.


The above code uses Ecmascript 262 edition 5 code which works in all modern browsers. If you want to support legacy browsers you need to load any of the various ES5 shim libraries

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • Nice turn, but `forEach` doesn't work everywhere. – Jasper Dec 04 '12 at 20:50
  • @Steve: Yes as I mentioned. It works like everywhere natively nowadays tho. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach – jAndy Dec 04 '12 at 20:52
  • @Steve: I wouldn't crash the party because of one browser (especially, not THAT browser). As I also mentioned, there a tons of great, very neat and small, shim libs which fix those legacy things. – jAndy Dec 04 '12 at 21:03