0

If I do the following,

var array = [];
array['foo'] = [];
console.log(array.length)   // => 0

the console.log prints 0 length.

Is there a way to push key-value to arrays in javascript? Is it a bad idea in general? Do I need to keep the length updated myself?

There is the option to use bracket notation as recommended here (SO - JavaScript Array Push key value).

But later in the code I want to push to the object/array and SO recommends to use arrays again (SO - Appending to an object).

So I'm not quite sure which datastructure to use. For my task I guess I need key-values. Its an async-task and I can't rely on the order in an unnamed array. But from what I read so far, it seems kind of a hack to append to objects, too.

Community
  • 1
  • 1
Stefan
  • 1,041
  • 1
  • 14
  • 28
  • 4
    `length` only counts the numeric array elements, not named properties. – Barmar Aug 26 '15 at 20:14
  • 1
    Usually named properties are used on objects, and arrays just have numeric indexes. Why are you trying to put properties on an array? – Barmar Aug 26 '15 at 20:15
  • `For my task I guess I need key-values.` Then use an object. Otherwise you'll need to expand your question to show us what exactly you are trying to do. – Matt Burland Aug 26 '15 at 20:15
  • @Bamar The data gets filled async, so I thought its the better way to access the right value later on. – Stefan Aug 26 '15 at 20:19
  • you should use as an object for key-value ; for checking length (number of prop.) check http://jsfiddle.net/maio/k8wz7862/ – maioman Aug 26 '15 at 20:21
  • Is there a way to "push" to an object? – Stefan Aug 26 '15 at 20:25
  • obj-properties don't have an index(like array entries) just a key – maioman Aug 26 '15 at 20:27
  • @Stefan: What do you mean? You "push" to an object using the bracket notation exactly how you have it `myObj[key] = value`. You question really isn't clear as to what you are actually trying to do. – Matt Burland Aug 26 '15 at 20:29
  • @Stefan: in technical terms you cannot "push" to an object. "Push" is only for arrays. You can "set" properties of an object. If you want to get a list of all the properties in an object you can do `Object.keys(obj)` which will give you an array of the keys. You can then do `Object.keys(obj).length` to get the number of keys in the object, if that's what you're asking for? – Daniel Lizik Aug 26 '15 at 20:31
  • @MattBurland: Yes, you are right. That was a stupid question. I guess thats then the way to go. – Stefan Aug 26 '15 at 20:32

2 Answers2

1

You can push an object:

var arr = [];
var obj = {foo: "bar"};
arr.push(obj);
arr.length; //1
arr[0].foo; //"bar"
Daniel Lizik
  • 3,058
  • 2
  • 20
  • 42
0

You should probably use a javascript object, which acts as a key/value pair data structure (like a HashMap or a Dictionary).

var data = {
  key: 'value'
}

data['key'] // 'value'
Object.keys(data).length // 1

For the sake of your future self, you should not add properties to an array nor try to simulate an array with an object.

Marcelo
  • 4,580
  • 7
  • 29
  • 46