0

Could someone possibly take a look at this: http://jsfiddle.net/VkFRU/4/

and explain the console output I'm seeing (which is "0", "true", "false" and "덜 읽기")?

The four individual console.log() calls output exactly what I expect, except for the the first one. Why is it saying length is 0?

The one in the $.each() iterator doesn't output anything. Why not?

1
  • Please post your code in the body of your question. Commented Jan 3, 2012 at 16:59

4 Answers 4

2

The four individual console.log() calls output exactly what I expect, except for the the first one. Why is it saying length is 0?

Only numerical properties are taken into account for length. 'ko' is not numerical.

From the specification:

The length property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.

Where array index is defined as:

A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1.

Therefore ko is not an array index.

The one in the $.each() iterator doesn't output anything. Why not?

Because jQuery recognizes the arguments as array an iterates over its values, probably in this way:

for(var i = 0; i < arg.length; i++)

You already noticed that translations.length is 0.


You are making the mistake to use an array as associative array. Use an object instead:

var translations = {};
translations['ko'] = {}; 

The reason why you can assign non-numerical properties to arrays is simply the fact that arrays are objects too. But that does not mean that you should use them as such. The special array methods don't apply to non-numerical properties.

I'd suggest you read more about arrays in the MDN JavaScript Guide.

Sign up to request clarification or add additional context in comments.

Comments

1

You need objects. Arrays in JavaScript are not Associative so you cannot access their members via names, just numbers

So either:

var translations = {};
translations.length = 0;
translations['ko'] = {};
translations.length++;
translations['ko']['Read more']    = '자세히보기';
translations['ko']['Read less']        = '덜 읽기';

console.log(translations.length);
console.log( 'ko' in translations );
console.log( 'wtf' in translations );
console.log( translations['ko']['Read less'] );

$.each(translations, function(lang, phrases) {
    console.log(lang);
});

But since objects in JS don't have a length attribute you'd have to add one

Comments

0

An associative array is actually an object. And, objects don't really have length.

On another note, jQuery would loop through with something similar to

for(a in b)

in the case of an object/associative array a = key, b = object, you would then get the value with

b[a]

To explain a little more what is happening:

var b = []; //this is an array "object"
b[0] = 1; //this would set the 0 index of b to 1
b['test'] = 'value';//this would set the property 'test' of the array object to 'value'

Also, javascript allows you to access properties in two ways, the . and []:

b['test'] = b.test;

Comments

0

You declare var translations = []; as array. The next statement translations['ko'] = []; actually adds a property ko to the instance translations. So you will be able to access it using translations.ko . To add one element to the array you have to use push statement which will increase the length of the array translations by adding one more element. see the demo here : http://jsfiddle.net/diode/VkFRU/5/

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.