Adding an object to an array in JavaScript can be done using the push or concat method. Here's how you can do it:
Using the push method:
```javascript
let arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let obj = { id: 3, name: 'Charlie' };
arr.push(obj);
```
In this example, the object `obj` is added to the end of the array `arr` using the push method.
Using the concat method:
```javascript
let arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let obj = { id: 3, name: 'Charlie' };
arr = arr.concat(obj);
```
In this example, the object `obj` is added to the end of the array `arr` using the concat method. Note that the `concat` method does not modify the original array, so you need to assign the result back to the original array.
Adding multiple objects to an array:
```javascript
let arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let objs = [{ id: 3, name: 'Charlie' }, { id: 4, name: 'David' }];
arr.push(...objs);
```
In this example, the spread operator `...` is used to add multiple objects from the `objs` array to the end of the `arr` array using the push method.
Using the spread operator and concat method:
```javascript
let arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
let objs = [{ id: 3, name: 'Charlie' }, { id: 4, name: 'David' }];
arr = arr.concat(...objs);
```
In this example, the spread operator `...` is used to pass each object in the `objs` array as individual arguments to the concat method, effectively adding them to the end of the `arr` array.
In summary, you can add an object to an array in JavaScript using the push or concat method. Additionally, you can use the spread operator to add multiple objects to an array using the push method or the concat method with the spread operator.