How Can I Add An Array Into Another Array In Javascript?
I have an two element array looking like this headers = [{x: 1, y: 2},{x: 2, y: 3}] I have another three element array that looks like: ans = [{r: true},{r: false},{r:true}] How
Solution 1:
You actually want to add another property to an element in an array. You can do it like this
headers[0].ans = ans;
Solution 2:
You could do something like this...
var headers = [{x: 1, y: 2},{x: 2, y: 3}];
var ans = [{r: true},{r: false},{r:true}];
// define what you want the newly added property to be calledvar collectionName = "ans";
// assign it only to the first item in the headers array
headers[0][collectionName] = ans;
// here are your resultsconsole.log(JSON.stringify(headers));
here is a working fiddle that will output the array for you to look at in an output div
Solution 3:
Simply assign your property to your object : headers[0].ans = ans
Solution 4:
varheaders= [{x:1, y:2},{x:2, y:3}],ans= [{r:true},{r:false},{r:true}];headers[0].ans=ans;
Post a Comment for "How Can I Add An Array Into Another Array In Javascript?"