rest operator - args functions p2

PHOTO EMBED

Thu Jun 20 2024 23:17:24 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #functions #...args #rest

   const testObj = {
    name: 'David',
    movie: 'fight club',
    active: true,
  };

  const testObj2 = {
    name: 'kevin',
    movie: 'Bladerunner',
    active: false,
  };



// using rest only
const combined = []; //empty array

  function makeArrayFromMUtipleObjects(...args) {
    for (let item of args) {
      // will have two separate objects
      combined.push(item); // push into the empty arry
    }
  }

  console.log(combined); 




// using rest and another param

 const anotherArray = [];

 function makeArray(array, ...itemsToAdd) {
    return array.concat(itemsToAdd);
  }

 const made = makeArray(anotherArray, testObj, testObj2);
 console.log(made);
content_copyCOPY