-1

I haven't touched Javascript in a while and now I'm having trouble with basic arrays.

params=new Array();
params['return']='json';
alert(params.length);

This always returns 0 when I'm expecting 1. What's wrong with this?

3
  • 4
    Only numeric indices will affect the .length of an Array. You're using params as though it was a plain Object, and Objects don't have an automatic .length. To count them, you could do Object.keys(params).length Commented Sep 18, 2013 at 23:25
  • Arrays are not associative in JS... Commented Sep 18, 2013 at 23:26
  • Does this answer your question? How to do associative array/hashing in JavaScript Commented Jun 27, 2024 at 9:23

4 Answers 4

2

Arrays use numerical indexes. When you use a string "index", it just adds a new property to the array object, but the new value isn't in the array.

Thus, the array is still empty, but you can access your value as you could with any other object.

Your code is equivalent to

var params = {}; //new object (not array)
params['return']='json'; //new property added to object
Sign up to request clarification or add additional context in comments.

Comments

1

A few things:

You forgot var:

var params = new Array();

But an array takes numeric indices, so params['return'] is not really a member of the array but of the object that represents the array, so it doesn't affect the length.

You could use an object but objects have no length:

var params = {};
params['return'] = 'json';

To get the length though you can count the keys in that object and get the length of the resulting array:

var len = Object.keys(params).length;

Comments

0

Javascript arrays don't hold key value pairs like objects do, so the length isn't incremented when you assign a value. They are however objects themselves, so you can assign values to its properties (in this case the return property).
You probably want params to be a plain object: params = {}

You need to count the properties, like this for example:

function dictionarySize(dict){
    var size = 0;
    for (key in dict){
        // In practice you'd probably want to filter using hasOwnProperty
        size++;
    }
    return size
}
alert(dictionarySize(params));

Or using a library like underscore.js:

_.size(params);

Object.keys is also an option, although it won't work in IE8 and older.

If you don't need an key value pairs use params.push:

params=new Array();
params.push('json')
alert(params.length); // 1

Comments

0

You can create an array, push stuff on it, and assign properties to values of it like so:

var params=[];
params.push('firstly');
params[0]="jonson";
params['return']="fredy"
params.newone="json";
params.push('Carl');

NOW, if you do:

console.log(params.length,params);

the result of that is:

2 ["jonson", "Carl", return: "fredy", newone: "json"]

SO, you see "firstly" was replaced by "jonson" in the [0] - so the "pushed" value is addresed by the numerical [0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.