push object to array code example

Example 1: javascript add object to array

var object = {'Name'};
var array = [ ]; // Create empty array

// SIMPLE
	array.push(object); // ADDS OBJECT TO ARRAY (AT THE END)
	// or
	array.unshift(object); // ADDS OBJECT TO ARRAY (AT THE START)

// ADVANCED
	array.splice(position, 0, object);
	// ADDS OBJECT TO THE ARRAY (AT POSITION)

		// Position values: 0=1st, 1=2nd, etc.
		// The 0 says: "remove 0 objects at position"

Example 2: push in object javascript

//push in object javascript
var data = [];
// ...
data[0] = { "ID": "1", "Status": "Valid" };
data[1] = { "ID": "2", "Status": "Invalid" };
// ...
var tempData = [];
for ( var index=0; index<data.length; index++ ) {
    if ( data[index].Status == "Valid" ) {
        tempData.push( data );
    }
}
data = tempData;

Example 3: how to add object to list in javascript

var a=[]
var b={};
a.push(b);

Example 4: push object into array javascript

var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);

Example 5: add object to array javascript

let obj = { name: 'Bob' };
let arr = [{ name: 'John' }];

// add obj to array
arr = [...arr, obj];

console.log(arr) // [{ name: 'Bob' }, { name: 'John' }];

Example 6: how to push array object name javascript

function getKeyValue(object) {
    return Object.keys(object).reduce(function (result, key) {
        return result.concat(
            object[key] && typeof object[key] === 'object' ?
            getKeyValue(object[key]) :
            [[key, object[key]]]
        );
    }, []);
}

var data = { id: 23, name: "Jacob", link: { rel: "self", link: "www.abc.com", }, company: { data: { id: 1, ref: 324 } } };

console.log(getKeyValue(data));