How to push an object in an Array
const animals = ['pigs', 'goats', 'sheep'];
animals.push({animal: 'cows'});
console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]
Answer from Soham on Stack OverflowVideos
How to push an object in an Array
const animals = ['pigs', 'goats', 'sheep'];
animals.push({animal: 'cows'});
console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]
I think like this:
var CatTitle = ['Travel', 'Daily Needs','Food & Beverages','Lifestyle','Gadget & Entertainment','Others'];
var myObj = {Coupon exp : 'xxx', couponcode : 'xxx'};
var newObject = {};
var newArray = [];
var i;
for(i=0; i < CatTitle.length; i++) {
var dump = {
CatTitle[i]: myObj
}
newArray.push(dump);
}
newObject = newArray;
Use the concat function, like so:
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).
In ECMAScript 6, you can use the Spread syntax:
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);
console.log(arr1)
Spread syntax is available in all major browsers (that excludes IE11). For the current compatibility, see this (continuously updated) compatibility table.
However, see Jack Giffin's reply below for more comments on performance. It seems concat is still better and faster than the spread operator.
Use the Array.prototype.push method to append values to the end of an array:
Copy// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can use the push() function to append more than one value to an array in a single call:
Copy// initialize array
var arr = ["Hi", "Hello", "Bonjour", "Hola"];
// append multiple values to the array
arr.push("Salut", "Hey");
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Note that the push() method returns the updated length of the array.
Update
If you want to add the items of one array to another array, you can use firstArray.concat(secondArray):
Copyvar arr = [
"apple",
"banana",
"cherry"
];
// Do not forget to assign the result as, unlike push, concat does not change the existing array
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Update
Just an addition to this answer if you want to prepend any value to the start of an array (i.e. first index) then you can use Array.prototype.unshift for this purpose.
Copyvar arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
It also supports appending multiple values at once just like push.
Update
Another way with ES6 syntax is to return a new array with the spread syntax. This leaves the original array unchanged, but returns a new array with new items appended or prepended, compliant with the spirit of functional programming.
Copyconst arr1 = [
"Hi",
"Hello",
"Bonjour",
];
const arr2 = [
"Ciao",
"Hej",
"Merhaba",
];
const newArr1 = [
...arr1,
"Salut",
];
const newArr2 = [
"Salut",
...arr2,
];
const newArr3 = [
...arr1,
...arr2,
];
console.log(newArr1, newArr2, newArr3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you're only appending a single variable, then push() works just fine. If you need to append another array, use concat():
Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
var ar3 = ar1.concat(ar2);
alert(ar1);
alert(ar2);
alert(ar3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The concat does not affect ar1 and ar2 unless reassigned, for example:
Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
ar1 = ar1.concat(ar2);
alert(ar1);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
There is a lot of great information on JavaScript Reference.
Arrays in javascript are not the same as an array in C. In C you'll allocate a new array with type and a fixed size and if you want to alter the size you'll have to create a new one. For javascript arrays on the other hand if you have a look at the description of arrays over at MDN you'll see this line.
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array
So while it is called "array" it is more like a list which lets you resize it freely (and also store anything, because javascript).
There is also another question about the semantics of the push/pop methods here on SO that can shine some more light on those: How does the Javascript Array Push code work internally
A JavaScript Array is not like a C array, it's more like a C++ std::vector. It grows in length by dynamically allocating memory when needed.
As stated in the reference documentation for std::vector:
Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. [...] Reallocations are usually costly operations in terms of performance.