How to add items to array in Node.js [SOLVED]

Different methods to add items to array in Node.js

Updating data is the crux of building applications, and depending on the type of data structure in place, the means of carrying out such operation will differ. Arrays are one of the many data structures within JavaScript that we will work with a lot, and updating it by adding items to it is essential to know.

In this article, we will how to add items to array in Node.js and make use of three methods - push, unshift and concat - to achieve this

Advertisement

 

Use the push method

To add items to array, the first method that can be used and is more intuitive is the push method. With this method, you can more than one element or item to the array, and also, it returns the updated length of the array.

Let’s show the push method by update an array of math scores and logging the array before and after the addition of the new score by the push() method.

let scores = [34, 56, 78, 93, 42, 89, 55];
console.log("The array before 'push' method operation\\n", scores);
scores.push(45);
console.log("The array after 'push' method operation\\n", scores);

Output

The array before 'push' method operation
 [
  34, 56, 78, 93,
  42, 89, 55
]
The array after 'push' method operation
 [
  34, 56, 78, 93,
  42, 89, 55, 45
]

Let’s add more than one item and store the return value of the push() method.

let scores = [34, 56, 78, 93, 42, 89, 55];
push = scores.push(34, 67, 32);

console.log(push);
console.log(scores);

Output

10
[
  34, 56, 78, 93, 42,
  89, 55, 34, 67, 32
]

The first line shows the length of the array (10) which is verifiable by the array logged below it.

Advertisement

 

Use the unshift method

However, if we need to add the items to the beginning of the array, we can’t make use of the push method, that’s where unshift method comes in.

With the unshift method, we can add items to array to the beginning of it. Just like push, it returns the new length of the array and can add more than one element to the array.

Let’s illustrate this by adding new (crucial) tasks to the beginning of the task array.

let tasks = ["Buy Mango", "Read Chemistry Modules", "Practise JavaScript"];
console.log("The array before 'unshift' method operation\\n", tasks);

tasks.unshift("Visit Grandpa in Hospital", "Meet Girlfriend");
console.log("The array after 'unshift' method operation\\n", tasks);

Output

The array before 'unshift' method operation
 [ 'Buy Mango', 'Read Chemistry Modules', 'Practise JavaScript' ]
The array after 'unshift' method operation
 [
  'Visit Grandpa in Hospital',
  'Meet Girlfriend',
  'Buy Mango',
  'Read Chemistry Modules',
  'Practise JavaScript'
]

 

Use the concat method

Instead of adding singular elements, we might have all the items we want inside an array that way we can add an array to another array. To carry out this operation, we can make use of the concat method.

Unlike push or unshift, this method creates (or returns) a new array with the element of the elements of both arrays.

Advertisement

Let’s add items to array by merging two data arrays (daneData and femiData) and log the result of the array merge.

const daneData = [23, "Java", true, "L23"];
const femiData = [21, "JavaScript", false, "L15"];

const combinedData = daneData.concat(femiData);

console.log(combinedData);

Output

[ 23, 'Java', true, 'L23', 21, 'JavaScript', false, 'L15' ]

 

Summary

Depending on how you want to add items to array in node.js, you have different methods for that. If you want to add items to back of the array, the push method is there, and if you want to add items to the front of the array, the unshift method works for you. However, if you want to add another array to an array, you can make use of the concat method.

 

References

Array - JavaScript | MDN (mozilla.org)
[Array.prototype.push() - JavaScript | MDN (mozilla.org)]
Array.prototype.unshift() - JavaScript | MDN (mozilla.org)
Array.prototype.concat() - JavaScript | MDN (mozilla.org)

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X