0

Have JS array w/o keys

code 1

var psnb_arr=['14-007', '14-008', '14-009'];
console.log(psnb_arr);

//output in firebug: ["14-007", "14-008", "14-009"]

code 2

for (var i in psnb_arr) {
  var psnb=psnb_arr[i];
  console.log(i+'-'+psnb);
}

//wierd output in firebug:
0-14-007
1-14-008
2-14-009

copy-function (start,length){start=start||0;if(start<0)start=this.length+start;

length=length||(this.length-start);var newArray=[];for(var i=0;i<length;i++)newArray[i]=this[start++];return newArray;}

remove-function (item){var i=0;var len=this.length;while(i<len){if(this[i]===item){this.splice(i,1);len--;}else{i++;}} return this;}

contains-function (item,from){return this.indexOf(item,from)!=-1;}


...

what is this wierd output in firebug for console.log within the loop? I mean the text that is coming right after "2-14-009" - "copy-function..."

5
  • 3
    what is so wired about it, its coming correct output. Commented Feb 14, 2014 at 21:00
  • i = the "0-" on the first output, just as it's written in your code. Commented Feb 14, 2014 at 21:00
  • Your "object without keys" is just an array. It does have keys, they are numeric. Commented Feb 14, 2014 at 21:01
  • I mean the text that is coming right after "2-14-009" - "copy-function..." Commented Feb 14, 2014 at 21:01
  • Can you make a Fiddle to reproduce your problem? You dont have this in another browser? I just get the array output, not the other stuff. What version Firefox / firebug are you using? Commented Feb 14, 2014 at 21:02

1 Answer 1

2

You do not have an object, nor do you have an associative array (these no not exist in JavaScript). What you have in a "normal" (numeric) array. It does have keys; the array indexes (that's where the 0-, etc. are coming from).

The problem you are seeing is because you are using for...in on an array. This is a bad practice. The Array prototype can contain properties (sometimes JavaScript libraries add these), so you are iterating over all of those properties, not just the array values.

You want to use a normal for loop here.

for(var i = 0; i < psnb_arr.length; i++){
    var psnb=psnb_arr[i];
    console.log(i+'-'+psnb);
}
Sign up to request clarification or add additional context in comments.

7 Comments

@putvande: Yeah, I'm fully aware. I explained exactly why that's happening and how to fix it. What's the problem?
thank you, I didn't know about "sometimes JavaScript libraries add these"
@ihtus: Are you using something like Prototype or an ES5 shim? That's normally where this comes from. They add methods to Array.prototype.
as another option hasOwnProperty can fix the issue above, but anyway @RocketHazmat explanation is OK
@EugeneP: Yes, you can add hasOwnProperty to the for..in loop, but it's easier (and I think better practice) to just use a normal for loop (or .forEach()). :-)
|

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.