0

Here is sample code:

console.log('params is empty: '+(params == ''));
console.log('params: '+params);
console.log('df: '+params['df']);
$.each(params, function(p_name, p_val){
    console.log(p_name+': '+p_val);
});

And responce:

params is empty: true
params:
df: 15.03.2012

How this can be?

2
  • Does params perchance have a toString method that evaluates to ""? Commented Mar 13, 2012 at 17:06
  • @rjz — Since the question says "array" and gives no suggestion that it has any property that isn't "df", then it seems likely. Commented Mar 13, 2012 at 17:13

2 Answers 2

3

Presumably (it has to be that way as you are showing use a predefined variable so we can't know what is actually in it except by drawing conclusions from the tests you are performing on it) because params is an array and not an object.

Arrays are designed to hold an ordered sequence of values with numeric keys.

Objects have values with (relatively) arbitrary keys.

Stringifying an array only joins the numeric keys. So comparing it to an empty string will only give a false value if there are numeric keys.

$.each is noting that it is iterating over an array and only hits the numeric keys.

df is not a numeric key.

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

Comments

1

Nothing strange. Params is just an object (of builtin type Array).

console.log('params is empty: '+(params == '')); //result of Array.toString on empty array = ""

console.log('params: '+params); //result of Array.toString on empty array = ""

console.log('df: '+params['df']); //if params is an object and you do params.df='15.03.2012' then params['df']='15.03.2012'. 

$.each(params, function(p_name, p_val){
    console.log(p_name+': '+p_val);
});

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.