JavaScript Program to Append an Object to An Array Using spread Operator Method Full Project For Beginners

 

 

script.js

 

 

// program to append an object to an array

function insertObject(arr, obj) {

   // append object
    arr = [...arr, object];
    
    console.log(arr);
}

// original array
let array = [1, 2, 3];

// object to add
let object = {x: 12, y: 8};

// call the function
insertObject(array, object);

Leave a Reply