Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
4 views
javascript push
in JavaScript Programming by (2.5k points)

3 Answers

0 votes

JavaScript push() :

Example

If we need to add new item to existing array

var animals= ["Cow""Dog""Owl""Lion"];
animals.push("Cat");

Definition of Push()

So push method extends items in array and also increases its length:

Note: unshift() method is used to at the items at beginning of an array.

Syntax

array.push(item1item2, ..., itemX)

Parameter Values

ParameterDescription
item1item2, ..., itemXRequired. The item(s) to add to the array

Examples

Example

To add more than one items:

var animals= ["Cow""Dog""Owl""Lion"];
animals.push("Cat","Monkey");
by (8.5k points)
0 votes
#Merging two arrays

const cars = ['Honda', 'SUX'];

const newbikes= ['Mar', 'HD'];

Array.prototype.push.apply(cars , newbikes);

#Add Multiple Items in an Array

const cars= ['H', 'T'];

const newCars= ['R', 'G'];

artists.push(newCars);

#Javascript push object to array

arrObj = [

  {

    name: 'Sam',

    age: 56

  },

  {

    name: 'Love',

    age: 54

  }

];

arrObj.push({

  name: 'Sab',

  age: 47

});

console.log(arrObj);

#Javascript push array into array

// app.js

arrArray = [

  ['K', 'A'],

  ['R', 'D']

];

arrArray.push([

  'Te', 'R'

]);

console.log(arrArray)

#Javascript multidimensional array push

#Add an element at specified an index

// app.js

let list = ["f", "b"];

list[2] = "b";

list[3] = "q";

console.log(list)
by (8.5k points)
0 votes

See following links for more info:

Push an array into the object in JavaScript

by (8.5k points)
...