0

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?

3
  • What result did you expected to get? Commented May 29, 2017 at 12:13
  • [1,11,111,12,122,1222,123,13,133,1333] Commented May 29, 2017 at 12:54
  • see if[123] is not an array then why [13,133,1333] is represented as array Commented May 29, 2017 at 12:55

4 Answers 4

1

[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);

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

2 Comments

then output should be [1,11,111,12,122,122,Array(1),Array(3)] rite?
no it will be [1, 11, 111, 12, 122, 1222, 123, 13, 133, 1333]
1

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);

Comments

0

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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.