Jigong Bagong
Jigong Bagong

Reputation: 85

Why there's a total index number of arrays in concat result

var a = ['a', 'b', 'c'];
var b = ['d', 'e', 'f'];
var c = ['g', 'h', 'i'];
var d = [a, b, c];
for (e in d) var f = d.concat(e);
console.log(f); //[Array[3], Array[3], Array[3], "2"]

Why there's 2 in there? How to remove that 2, before the result come out? (not alter the result)

Upvotes: 0

Views: 61

Answers (3)

Umesh Aawte
Umesh Aawte

Reputation: 4690

This is because of your concat operation, here e is the index of element in array d, Try the code below you will get an idea

var a = ['a', 'b', 'c'];
var b = ['d', 'e', 'f'];
var c = ['g', 'h', 'i'];
var d = [a, b, c];
var f ;
for (e in d){
    console.log(e);
    f = d.concat(e);
    console.log(f);
}
console.log(f); //[Array[3], Array[3], Array[3], "2"]

Still it is not clear what output you are expecting.


Edit 1 Expected output is d array. As you already defined two dimentional array d having all above there is no need to concate it.

console.log(d);

Edit 2 If you what to copy array using this code there is wayaround

var a = ['a', 'b', 'c'];
var b = ['d', 'e', 'f'];
var c = ['g', 'h', 'i'];
var d = [a, b, c];
var f = [];
for (e in d){
    console.log(e);
    f[e] = d[e];
    console.log(f);
}
console.log(f); //[Array[3], Array[3], Array[3]]

Above code will copy array d in array f

Upvotes: 0

KooiInc
KooiInc

Reputation: 122916

"2" is the length property of the resulting array. It is included, because you loop using for ... in. If you use for (var i=0; i<d.length; i++) {...}, it will not be included. See snippet. Anyway, it seems to me that you could simplify the whole enchillada using var f = d.slice().

Citing MDN:

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.
Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.

See also ...

var a = ['a', 'b', 'c'];
var b = ['d', 'e', 'f'];
var c = ['g', 'h', 'i'];
var d = [a, b, c];
var f = [];
for (var i=0; i < d.length; i+=1) {
   f.push(d[i]);
}
document.querySelector("#result").textContent = JSON.stringify(f, null, " ");
<pre id="result"></pre>

Upvotes: 2

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

Well, based on other answers, you now know the reasoning behind why "2" in your array.

I am trying to target your actual problem and I might be hypothetical, however, it seems you want to concatenate 3 arrays.

If my understanding is correct, you can try

var a = ['a', 'b', 'c'];
var b = ['d', 'e', 'f'];
var c = ['g', 'h', 'i'];
var f = a.concat(b).concat(c);
console.log(f); // ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

Upvotes: 0

Related Questions