js push onto object array code example
Example 1: js add item to array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
Example 2: jquery add element to array
var ids = [];
$(document).ready(function($) {
var $div = $("<div id='hexCodes'></div>").appendTo(document.body), code;
$(".color_cell").each(function() {
code = $(this).attr('id');
ids.push(code);
$div.append(code + "<br />");
});
});
Example 3: js push in object
let obj = {};
let objToAdd1 = { prop1: "1", prop2: "2" };
let objToAdd2 = { prop3: "3", prop4: "4" };
// obj variabile could be empty or not, it's the same
obj = { ...obj, ...objToAdd1 };
obj = { ...obj, ...objToAdd2 };
// Note that i used the spread operator... This syntax is available
// only for the most recent js ES (from ES6 on, if i'm not wrong) :)
console.log(obj);
Example 4: javascript array push object
var myArray = [];
myArray.push("Example");
myArray.push("text");