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]]
asked Mar 3, 2014 at 10:12
User1038
4653 gold badges7 silver badges17 bronze badges
3 Answers 3
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 );
answered Mar 3, 2014 at 10:24
Nevin
3,3682 gold badges29 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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]);
answered Mar 3, 2014 at 10:15
durum
3,4041 gold badge27 silver badges31 bronze badges
Comments
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);
4 Comments
User1038
and how to push c into JSON
Sam
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.keyOfYourChoiceMoob
If by "push to JSON" you mean "convert to a JSON String" you can use
JSON.stringify(c) in supporting browsers User1038
@sam sorry for that confusing statement, I want to add it to an object Thanks sam
Explore related questions
See similar questions with these tags.
lang-js
cto equal[[1,2,3,4,5],[,6,7,8,9,10]]or[1,2,3,4,5,6,7,8,9,10]?.push()