0

How to push array in another array using JavaScript?

Example:-

var a = [1,2,3,4,5]
var b = [6,7,8,9,10]
i want to push a & b into c
var c =[[1,2,3,4,5],[6,7,8,9,10]]
3
  • 2
    Please clarify: Do you want c to equal [[1,2,3,4,5],[,6,7,8,9,10]] or [1,2,3,4,5,6,7,8,9,10]? Commented Mar 3, 2014 at 10:18
  • i want c like this [[1,2,3,4,5],[6,7,8,9,10]] Commented Mar 3, 2014 at 10:20
  • @Phoenix use .push() Commented Mar 3, 2014 at 10:20

3 Answers 3

2

Basically what sam said is enough:

var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);

To convert to JSON:

var myJsonString = JSON.stringify(yourArray);

And to get it into an object :

var arrFromJson = JSON.parse( myJsonString );

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

Comments

1

Simple, you just have to create another array to do it:

var c = [a,b];

Then, you can use .push to add more arrays.

c.push([11,12,13]);

Comments

1
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);

4 Comments

and how to push c into JSON
What do you mean by "push into JSON"? if you want to add it to an object you can do yourObject['keyOfYourChoice']=c; and access it with yourObject['keyOfYourChoice'] or yourObject.keyOfYourChoice
If by "push to JSON" you mean "convert to a JSON String" you can use JSON.stringify(c) in supporting browsers
@sam sorry for that confusing statement, I want to add it to an object Thanks sam

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.