Guru Deepak
Guru Deepak

Reputation: 35

JavaScript concat method

Using Array.prototype.concat(), when I write this code:

var g = [1, 11, 111];
var ga = [12, 122, 1222];
var gb = [13, 133, 1333];

var gc = g.concat(ga, [123, gb]);

console.log(gc);

It gives me the following result: [1, 11, 111, 12, 122, 1222, 123, Array(3)]

Why is Array(3) added to the resulting array?

Upvotes: 0

Views: 74

Answers (4)

user688
user688

Reputation: 361

This will help you!!

var g = [1,11,111];

var ga = [12,122,1222];

var gb = [13,133,1333];

var gc = g.concat(ga,123,gb);

       
console.log(gc);

Upvotes: 0

Dinesh undefined
Dinesh undefined

Reputation: 5546

Use ES6 style syntax spread

var g = [1,11,111];

var ga = [12,122,1222];

var gb = [13,133,1333];

var gc = [...g,...ga,123,...gb];

console.log(gc);

Upvotes: 1

TedMeftah
TedMeftah

Reputation: 1915

[123,gb] does not concat arrays it's an array inside of an array, what you want is this:

var g = [1,11,111];

var ga = [12,122,1222];

var gb = [13,133,1333];

var gc = g.concat(ga,[123].concat(gb));
//or better
var gc2 = g.concat(ga, 123, gb);
console.log(gc, gc2);

Upvotes: 1

Quentin
Quentin

Reputation: 943163

One of the values in the array is an array with three items in it and you are looking at it using a tool that gives you a summary of the data rather than a fully expanded view of it.

Upvotes: 0

Related Questions