javascript - Why there's a total index number of arrays in concat result -
var = ['a', 'b', 'c']; var b = ['d', 'e', 'f']; var c = ['g', 'h', 'i']; var d = [a, b, c]; (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 remove 2, before result come out? (not alter result)
"2"
length
property of resulting array. included, because loop using for ... in
. if use for (var i=0; i<d.length; i++) {...}
, not included. see snippet. anyway, seems me simplify whole enchillada using var f = d.slice()
.
citing mdn:
array indexes enumerable properties integer names , otherwise identical general object properties. there no guarantee for...in return indexes in particular order , return enumerable properties, including non–integer names , inherited.
because order of iteration implementation-dependent, iterating on array may not visit elements in consistent order. therefore better use loop numeric index (or array.prototype.foreach() or for...of loop) when iterating on arrays order of access important.
var = ['a', 'b', 'c']; var b = ['d', 'e', 'f']; var c = ['g', 'h', 'i']; var d = [a, b, c]; var f = []; (var i=0; < d.length; i+=1) { f.push(d[i]); } document.queryselector("#result").textcontent = json.stringify(f, null, " ");
<pre id="result"></pre>
Comments
Post a Comment