1

I have some code that produces undefined values in an array making no sense.

here is the code:

gcc_py = 8;
gcc_pyd = 12;
var aci = 360/gcc_pyd;
var parcalar = new Array();
var renkler = new Array();

for(var i = 0;i<gcc_pyd;i++){   
    parcalar.push(aci);
    renkler.push('#000');
}

console.log(parcalar);
console.log(renkler);

console.log(parcalar) outputs this:

[ Object , Object , Object , Object , Object , Object , Object , Object , Object , Object , undefined × 2]

do you have any idea for the undefined values in the array?

12
  • 2
    It outputs [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30] for me. Commented Jun 21, 2012 at 18:10
  • 3
    Also, please change new Array(); to [] it's considered better practice Commented Jun 21, 2012 at 18:11
  • 1
    I got valid results as well. What are you testing this in? Commented Jun 21, 2012 at 18:11
  • 1
    @exculuber: Is the the exact code you're running? Commented Jun 21, 2012 at 18:14
  • 1
    @exculuber: What are the contents of the Objects you're seeing in the console? Commented Jun 21, 2012 at 18:21

3 Answers 3

2

I guess you are changing the arrays afterwards. The console will reflect these changes, e.g. then showing objects instead of numbers and strings. Also, when deleting properties from an array (via delete, see Deleting array elements in JavaScript - delete vs splice), they will still show up as initialised but empty (see What is "undefined x 1" in JavaScript?).

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

2 Comments

yes, after these lines there is a function call in which these arrays are passed. The function changes the array. Thank you for the answer. Do you have further information about this concept may be a documentation, link, etc?
documentation about what? For console you'll find getfirebug.com/logging or patik.com/blog/complete-cross-browser-console-log helpful.
0

The output for me shows

[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]

["#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000"]

Maybe you've added some .prototype stuff to Array somewhere else in the code?

1 Comment

I'm using raphael.js and its plugin piechart.js
0

When I run the same configuration:

<script>
  var gcc_py = 8;
  var gcc_pyd = 12;
  var aci = 360 / gcc_pyd;
  var parcalar = new Array();
  var renkler = new Array();

  for(var i = 0; i < gcc_pyd; i++){
      parcalar.push(aci);
      renkler.push('#000');
  }

  console.log(parcalar);
  console.log(renkler);
</script>

I get:

[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
["#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000", "#000"]

not sure what you're seeing.

1 Comment

My console outputs it as an array of objects do you have any idea for that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.