Navigate back to the homepage

How to append array to object in javascript?

Infinitbility
JavaScript
May 19th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we will learn to add an array in an object in javascript, here we will use some common syntax of an object to store an array to an object.

To add an array in the object, we will use object add elements syntax, where we have to just assign an array to an object like assign to any variable.

Well, let’s start the today’s tutorial How to append an array to object in javascript?

Here, we will create the first two variables with sample data of an object and array.

1// your sample array
2const arrList = ['A', 'B', 'C'];
3
4// your sample object
5const objList = {
6 number: [1, 2, 3]
7}

Now, we will assign array data in the object, and as we know we need to define some key-like variable to store an array in an object.

Okay, then we will add an array as a alphabets key.

Append array at the creation of an object

Below example based on the initial time of the object like when we create an object, then how we can add an array to the object.

1// your sample array
2const arrList = ['A', 'B', 'C'];
3
4// your sample object
5const objList = {
6 numbers: [1, 2, 3],
7 alphabets: arrList,
8}
9
10console.log(objList)
javascript, append array to object at intial time example
javascript, append array to object at intial time example

Append array after the creation of an object

Below example based on how can we add an array after an object is declared and used.

Let’s see, getting more interesting.

1// your sample array
2const arrList = ['A', 'B', 'C'];
3
4// your sample object
5const objList = {
6 numbers: [1, 2, 3],
7 alphabets: arrList,
8}
9
10console.log(objList)
javascript, append array to object after creation example
javascript, append array to object after creation example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get all values from nested json object in javascript?

Example of get values keys from nested json object in javascript with step by step guide.

May 18th, 2022 · 1 min read

How to get count of json object in javascript?

Example of get count of json object in javascript with step by step guide.

May 18th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]