1

I want to display an array without showing of indexes. The for loop returns the array indexes which is not showing in usual declaration. I want to send an array like [1,2,3 ...] but after retrieving from for loop, I haven't the above format. How can I store my values as above.

var a = [];
for (var i = 1; i < 8; i++) {
    a[i] = i;
};
console.log(a);

Outputs:

[1: 1, 2: 2 ...]

Desired output:

[1,2,3]// same as console.log([1,2,3])
dfsq
  • 191,768
  • 25
  • 236
  • 258
developer
  • 668
  • 1
  • 6
  • 24
  • 1
    The resulted array is correct (except it holds `undefined` at the first index). The output in the console is just formatted in a different way. – Teemu Feb 15 '15 at 18:02

4 Answers4

3

Array indices start at zero, your loop starts at 1, with index 0 missing you have a sparse array that's why you get that output, you can use push to add values to an array without using the index.

var a = [];
for (var i = 1; i < 8; i++) {
    a.push(i);
};
console.log(a);
Musa
  • 96,336
  • 17
  • 118
  • 137
2

The problem is that you start your array with 1 index, making initial 0 position being empty (so called "hole" in array). Basically you treat array as normal object (which you can do of course but it defeats the purpose of array structure) - and because of this browser console.log decides to shows you keys, as it thinks that you want to see object keys as well as its values.

You need to push values to array:

var a = [];
for (var i = 1; i < 8; i++) {
    a.push(i);
};
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

I have to disagree with the answers provided here. The best way to do something like this is:

var a = new Array(7);
for (var i = 0; i < a.length; i++) {
    a[i] = i + 1;
}
console.log(a);
Acidic
  • 6,154
  • 12
  • 46
  • 80
  • Can you please explain, why would this be the best way? This doesn't even put the numbers OP needs to the array. – Teemu Feb 15 '15 at 18:18
  • Because this way allocates the required size in one go, while creating a 0-length array and increasing it size gradually will most likely require re-allocating memory blocks multiple times (depends on the final size of the array). – Acidic Feb 15 '15 at 18:19
  • I'm appreciating your explanation. Thanks. – developer Feb 15 '15 at 18:20
  • Afaik defining an empty array using `[]` is preferred nowadays. `new Array()` should be avoided because of some inconsistencies of it when using arguments. Looks like you've fixed the content issue though. – Teemu Feb 15 '15 at 18:23
  • @Teemu Yes, I did fix the content, thanks to your comment. What inconsistencies are there with `new Array(size)`? – Acidic Feb 15 '15 at 18:25
  • [A result of a quick-search](http://stackoverflow.com/a/4292494/1169519). What is not actually told in that answer (or rather is slightly erranously implemented), is that you can't create an array containing a single number member by using `new` operator. That's an inconsistency, since you can create an array with two or more numbers ... About the memory allocation, JS doesn't have real arrays, they are just objects. – Teemu Feb 15 '15 at 18:36
  • @Teemu I don't really understand how that is an inconsistency, seems to me like the behavior is perfectly documented and predictable. Either way, using the `Array` constructor when the array literal syntax could be used for the same effect seems pointless to me (unless the `Array` constructor is shown to be faster). The point here is that the `Array` constructor can allocated the required memory block at once, instead of requiring multiple allocations – something that can hurt performance exponentially. – Acidic Feb 15 '15 at 18:41
  • If a guy who's involved in developing JavaScript language [recommends `[]`](http://javascript.crockford.com/code.html) (see "Bonus Suggestions" at the near of the end of the article), I doubt using it would cause any performance problems. – Teemu Feb 15 '15 at 18:51
  • @Teemu Here's a simple test: http://jsperf.com/array-growth-vs-pre-allocation. See for yourself the performance benefits. – Acidic Feb 15 '15 at 18:56
  • Looks like you're testing `a.push()` against `a[i] = i` ... You can use a regular assignment with `a = []` as well. – Teemu Feb 15 '15 at 19:00
  • @Teemu That's because the Array literal syntax does not allow you to set the size of the array without providing the elements themselves directly in the code – which is exactly the advantage of the `Array` constructor and the reason why I suggested using it. – Acidic Feb 15 '15 at 19:02
0

Your code is making each index equal to i, so use it this way

var a = [];
for (var i = 1; i < 8; i++) {
a.push(i);
};
console.log(a);
void
  • 36,090
  • 8
  • 62
  • 107