/    Sign up×
Community /Pin to ProfileBookmark

Not able to append two arrays

Dear all,
I am declaring array 1 and aray 2 as shown below.

I am trying to append array1 with array 2 using push, but it not doing correctly and throwing undefined for Id and index 2 and 3. Please advice or share your ideas.Thank you for your support and help.
================================================

let array1=[{id:1, name:”John”}, {id:2, name:”Tom”}];
let array2=[{id:3, name:”Harry”},{id:4, name:”Poter”}];
array1.push(array1,array2);
for(let i=0;i<array1.length;i++)
{
console.log(“array 1″+”index:”+i+”id:”+array1[i][“id”]);

}

================================================

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@NR2authorMar 09.2020 — I got the answer, I need to use Array.prototype.push.apply(array1,array2);
Copy linkTweet thisAlerts:
@daveyerwinMar 09.2020 — @NR2#1615916
``<i>
</i>&lt;script&gt;
let array1=[{id:1, name:"John"}, {id:2, name:"Tom"}];
let array2=[{id:3, name:"Harry"},{id:4, name:"Poter"}];
array1 = array1.concat(array2);
for(let i=0;i&lt;array1.length;i++){
console.log("array 1"+"index: "+i+" id: "+array1[i]["id"]);
}<i>
</i>
``

</script>
Copy linkTweet thisAlerts:
@emilypowersMar 31.2020 — I see.That have sense.Thank you for your time and effort

[run 3](https://run3freegame.com)
×

Success!

Help @NR2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...