js array oush code example

Example 1: how to push items in array in javascript

let items = [1, 2, 3]
items.push(4); // items = [1, 2, 3, 4]

Example 2: javascript array clear

var cars = ["mazda","honda","tesla"];
	cars = []; // clear/empty the array

Example 3: 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 4: js push

let arr = [9, 4, 3, 18]
arr.push(30)
console.log(arr) // [9, 4, 3, 18, 30]

Example 5: push javascript

/*The push() method adds elements to the end of an array, and unshift() adds
elements to the beginning.*/
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];

romanNumerals.push(twentyThree);
// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']

romanNumerals.unshift('XIX', 'XX');
// now equals ['XIX', 'XX', 'XXI', 'XXII']