Warning: session_start(): open(/tmp/sess_4909a575509205367a5bb053f53e5bad, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Push An Object To An Array In JavaScript - LearnShareIT

How To Push An Object To An Array In JavaScript

Push an object to an array in JavaScript

Push an object to an array in JavaScript sounds interesting, doesn’t it? Now we’ll clarify how to put an object into an array in javascript. This is a case we often encounter in working with data from the server side, and there are many ways we can solve this problem. Below are the cases encountered and solutions.

Push an object to an array in JavaScript

Solution 1: Using the push() method

We will first create an empty array with no elements named array. We then create a student object with keys id, name, and age and assign them values.

Let’s look at an example to push an object to an array in JavaScript easier to understand.

const array = [];

const student = {
  id: 123,
  name: "VanVi",
  age: 20,
};

We will learn about the syntax to do this.

Syntax:

array.push(object)

Parameter:

  • object: The object you want to add to the array.
  • array: This array will contain the added object.
const array = [];

const student = {
  id: 123,
  name: "VanVi",
  age: 20,
};

array.push(student);
console.log(array);

Output:

 [{id:123, name:'VanVi', age: 20,}]

So we have done adding an object to an array.

You can do it more succinctly when doing it directly as follows.

const array = [];

array.push({
  id: 123,
  name: "VanVi",
  age: 20,
});

console.log(array);

And it will give you the same result as above.

In this example, we created an empty array. It will still be true no matter how many elements our array originally had.

const array = ['Javascript','ReactJs'];

const student = {
  id: 123,
  name: "VanVi",
  age: 20,
};

array.push(student);
console.log(array);

Output:

 ['Javascript','ReactJs',{id:123, name:'VanVi', age: 20,}]

And note that when you add an object to an array, that object will be added to the end of the array.

When you want to add multiple objects at once, you need to pass those objects as arguments to push() method and separate them by commas.

const array = ['Javascript','ReactJs'];

const student = {
  id: 123,
  name: "VanVi",
  age: 20,
};

const teacher = {
  id: 234,
  name: "Dung",
  age: 25,
}

array.push(student,teacher);
console.log(array);

Output: 

[
  'Javascript',
  'ReactJs',
  { id: 123, name: 'VanVi', age: 20 },
  { id: 234, name: 'Dung', age: 25 }
]

Method 2: Using the concat() method

It is also less common to add an object to an array. This method is usually used to concatenate two arrays but can also add an object to an array.

Parameter:

  • string1, string2: Are strings to be concatenated together.

In this example, we add two objects, student and teacher, into an array. We can add as many objects as possible.

const array = [];

const student = {
  id:123,
  name:"VanVi",
  age:20,
};

const teacher = {
  id:234,
  name:"Dung",
  age:25,
}

array1 = array.concat(student,teacher)
console.log(array1);

Output:

[
  { id: 123, name: 'VanVi', age: 20 },
  { id: 234, name: 'Dung', age: 25 }
]

Summary

Above are the most straightforward and concise ways and extended cases on how to push an object to an array in JavaScript. They will save you a lot of time and improve your working efficiency.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *