Append/push array into array in JavaScript

Today, We want to share with you javascript push array into array.In this post we will show you 5 Way to Append Item to Array in JavaScript, hear for JavaScript Array Insert – How to Add to an Array with the Push, Unshift, and Concat Functions we will give you demo and example for implement.In this post, we will learn about Array push key value pair Dynamically in javascript with an example.

JavaScript Array push() Method

Example 1: index.html

<!DOCTYPE html>
<html>
<body>

<p>Click the button to add new elements to the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var members = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = members;

function myFunction() {
  members.push("Kiwi", "Lemon", "Pineajay");
  document.getElementById("demo").innerHTML = members;
}
</script>

</body>
</html>

Example 2: Copy array items into another array

var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);

Example 3:

const arr = ['First item', 'Second item', 'Third item'];

arr.push('Fourth item');

console.log(arr); // ['First item', 'Second item', 'Third item', 'Fourth item']

Example 4:

const arr = ['First item', 'Second item', 'Third item'];

const arrLength = arr.push('Fourth item', 'Fifth item');

console.log(arrLength); // 5 
console.log(arr); // ['First item', 'Second item', 'Third item', 'Fourth item', 'Fifth item']

Example 5:

const arr = ['First item', 'Second item', 'Third item'];

const arrLength = arr.unshift('Urgent item 1', 'Urgent item 2');

console.log(arrLength); // 5 
console.log(arr); // ['Urgent item 1', 'Urgent item 2', 'First item', 'Second item', 'Third item']

Example 6: javascript pushing to an array

some_array = ["John", "Sally"];
some_array.push("Mike");

console.log(some_array); //output will be =>
["John", "Sally", "Mike"]

Example 7: how to append object in array javascript?

var select =[2,5,8];
var filerdata=[];
for (var i = 0; i < select.length; i++) {
  filerdata.push(this.state.data.find((record) => record.id == select[i]));
}

Example 8: add an element to an array javascript

var members = ["Banana", "Orange", "Apple", "Mango"];

members.push("Kiwi"); 

Example 9: what does results.push({}) nodejs mean

const mongoose = require('mongoose');
    var data = async function () {
         var array = [];
         const finalResults = await new Promise((resolve, reject) => {
            mongoose.connection.collection("organizations").find({}).toArray(function(err, result) {
              resolve(result);
           });
      });

     for(var i = 0; i < finalResults.length; i++)
     {
          var a = finalResults[i].name;
           array.push(a);
      }
        return array;
    };

    module.exports = {
        data: data,
    };

Example 10: js array add element

array.push(element)

Example 11: javascript append to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors

I hope you get an idea about push array to array javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  AngularJS populate Dynamic Dependent dropdown in PHP

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  jquery Multiple step form validation
  2. Read Also:  Array push key value pair Dynamically in javascript
  3. Read Also:  Laravel 6 display flash message on view
  4. Read Also:  javascript concatenate string and variable
  5. Read Also:  AngularJS push item to 0 index or first of $scope object
  6. Read Also:  JavaScript String Methods Functions Example
  7. Read Also:  Laravel Ajax Bootstrap Toggle switch Update MySQL
  8. Read Also:  Add Edit And Delete Records Using jQuery Ajax PHP And MySQL
  9. Read Also:  JQuery Push Key And Value Into An Array
  10. Read Also:  Create Scroll Back to Top Button using jQuery and CSS
CLOSEX