Hello Dev,
In this example we will see how to push array element in node.js example. I will share some example about push object in array in node.js. node.js array element push, push element in array in node.js, node.js array push key value.
So, let's start to implement how to push array element in node.js
push() is an array function from node.js that is used to add element to the end of an array.
Syntax:
array_name.push(element)
push_array = [2, 4, 6, 8, 10, 12];
push_array.push(14);
console.log(push_array);
Output :
Now see key and value push in array object.
key_val_arr = [
{id: 1, name: "dell" },
{id: 2, name: "hp" },
{id: 3, name: "apple" }
];
key_val_arr.push({id: 4, name: "lenovo"});
console.log(key_val_arr);
Output :
[
{ id: 1, name: 'dell' },
{ id: 2, name: 'hp' },
{ id: 3, name: 'apple' },
{ id: 4, name: 'lenovo' }
]
In this example add key and value in first position of array object using unshift() function.
key_val_arr = [
{ id: 1, name: "dell" },
{ id: 2, name: "hp" },
{ id: 3, name: "apple" }
];
key_val_arr.unshift({id: 4, name: "lenovo"});
console.log(key_val_arr);
Output :
[
{ id: 4, name: 'lenovo' },
{ id: 1, name: 'dell' },
{ id: 2, name: 'hp' },
{ id: 3, name: 'apple' }
]
You might also like :
In this article, we will see how to create a custom login page in django. how to allow user registration, login, an...
May-14-2022
In this article, we will see laravel 10 select2 autocomplete search using ajax. Here, we will learn about sele...
Apr-10-2023
In this article, we will see laravel 9 generate PDF from HTML using TCPDF. Here, we will learn how to integrate tcpdf in...
Jan-31-2023
In this article, we will see how to add multiple filter dropdowns in datatable. This example is almost identical to...
Jun-06-2022