Snippets Collections
const test = [3, 5, 6, 7, 8, 0, "", null, undefined, "testing"];


 test.forEach(listItem);

function listItem(item, number, total) {
    return `<li class="${number}">${item} ${number} of ${total}</li>`;
}


let htmlDOM = `<ul>${test.map((item, index, array) => listItem(item, `${index + 1}`, `${array.length}`)).join("")}</ul>`;
  

//console.log(htmlDOM);

/**

'<ul><li class="1">3 1 of 10</li><li class="2">5 2 of 10</li><li class="3">6 3 of 10</li><li class="4">7 4 of 10</li><li class="5">8 5 of 10</li><li class="6">0 6 of 10</li><li class="7"> 7 of 10</li><li class="8">null 8 of 10</li><li class="9">undefined 9 of 10</li><li class="10">testing 10 of 10</li></ul>'

*/


(function () {
  "use strict";

  const person = {
    name: "John Doe",
    age: 30,
    hobbies: ["reading", "traveling", "coding"],
  };

  const { hobbies, name, age } = person; // descructure

  hobbies.forEach((item, idx) => passInfo(item, idx, { name, age })); // pass function into the foreach

  function passInfo(item, idx, ...args) {
    console.log({ item, idx, args });
  }
})();



// another example

const test = [3, 5, 6, 7, 8, 0, "", null, undefined, "testing"];

test.forEach(listItem);


 function listItem(item, number, total) {
    return `<li class="${number}">${item} ${number} of ${total}</li>`;
  }
  const books = [
    {
      title: 'Algorithms',
      author: ['Robert Sedgewick', 'Kevin Wayne'],
      publisher: 'Addison-Wesley Professional',
      publicationDate: '2011-03-24',
      edition: 4,
      keywords: [
        'computer science',
        'programming',
        'algorithms',
        'data structures',
        'java',
        'math',
        'software',
        'engineering',
      ],
      pages: 976,
      format: 'hardcover',
      ISBN: '9780321573513',
      language: 'English',
      programmingLanguage: 'Java',
      onlineContent: true,
      thirdParty: {
        goodreads: {
          rating: 4.41,
          ratingsCount: 1733,
          reviewsCount: 63,
          fiveStarRatingCount: 976,
          oneStarRatingCount: 13,
        },
      },
      highlighted: true,
    },
    {
      title: 'Structure and Interpretation of Computer Programs',
      author: [
        'Harold Abelson',
        'Gerald Jay Sussman',
        'Julie Sussman (Contributor)',
      ],
      publisher: 'The MIT Press',
      publicationDate: '2022-04-12',
      edition: 2,
      keywords: [
        'computer science',
        'programming',
        'javascript',
        'software',
        'engineering',
      ],
      pages: 640,
      format: 'paperback',
      ISBN: '9780262543231',
      language: 'English',
      programmingLanguage: 'JavaScript',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 4.36,
          ratingsCount: 14,
          reviewsCount: 3,
          fiveStarRatingCount: 8,
          oneStarRatingCount: 0,
        },
      },
      highlighted: true,
    },
    {
      title: "Computer Systems: A Programmer's Perspective",
      author: ['Randal E. Bryant', "David Richard O'Hallaron"],
      publisher: 'Prentice Hall',
      publicationDate: '2002-01-01',
      edition: 1,
      keywords: [
        'computer science',
        'computer systems',
        'programming',
        'software',
        'C',
        'engineering',
      ],
      pages: 978,
      format: 'hardcover',
      ISBN: '9780130340740',
      language: 'English',
      programmingLanguage: 'C',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 4.44,
          ratingsCount: 1010,
          reviewsCount: 57,
          fiveStarRatingCount: 638,
          oneStarRatingCount: 16,
        },
      },
      highlighted: true,
    },
    {
      title: 'Operating System Concepts',
      author: ['Abraham Silberschatz', 'Peter B. Galvin', 'Greg Gagne'],
      publisher: 'John Wiley & Sons',
      publicationDate: '2004-12-14',
      edition: 10,
      keywords: [
        'computer science',
        'operating systems',
        'programming',
        'software',
        'C',
        'Java',
        'engineering',
      ],
      pages: 921,
      format: 'hardcover',
      ISBN: '9780471694663',
      language: 'English',
      programmingLanguage: 'C, Java',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 3.9,
          ratingsCount: 2131,
          reviewsCount: 114,
          fiveStarRatingCount: 728,
          oneStarRatingCount: 65,
        },
      },
    },
    {
      title: 'Engineering Mathematics',
      author: ['K.A. Stroud', 'Dexter J. Booth'],
      publisher: 'Palgrave',
      publicationDate: '2007-01-01',
      edition: 14,
      keywords: ['mathematics', 'engineering'],
      pages: 1288,
      format: 'paperback',
      ISBN: '9781403942463',
      language: 'English',
      programmingLanguage: null,
      onlineContent: true,
      thirdParty: {
        goodreads: {
          rating: 4.35,
          ratingsCount: 370,
          reviewsCount: 18,
          fiveStarRatingCount: 211,
          oneStarRatingCount: 6,
        },
      },
      highlighted: true,
    },
    {
      title: 'The Personal MBA: Master the Art of Business',
      author: 'Josh Kaufman',
      publisher: 'Portfolio',
      publicationDate: '2010-12-30',
      keywords: ['business'],
      pages: 416,
      format: 'hardcover',
      ISBN: '9781591843528',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.11,
          ratingsCount: 40119,
          reviewsCount: 1351,
          fiveStarRatingCount: 18033,
          oneStarRatingCount: 1090,
        },
      },
    },
    {
      title: 'Crafting Interpreters',
      author: 'Robert Nystrom',
      publisher: 'Genever Benning',
      publicationDate: '2021-07-28',
      keywords: [
        'computer science',
        'compilers',
        'engineering',
        'interpreters',
        'software',
        'engineering',
      ],
      pages: 865,
      format: 'paperback',
      ISBN: '9780990582939',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.7,
          ratingsCount: 253,
          reviewsCount: 23,
          fiveStarRatingCount: 193,
          oneStarRatingCount: 0,
        },
      },
    },
    {
      title: 'Deep Work: Rules for Focused Success in a Distracted World',
      author: 'Cal Newport',
      publisher: 'Grand Central Publishing',
      publicationDate: '2016-01-05',
      edition: 1,
      keywords: ['work', 'focus', 'personal development', 'business'],
      pages: 296,
      format: 'hardcover',
      ISBN: '9781455586691',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.19,
          ratingsCount: 144584,
          reviewsCount: 11598,
          fiveStarRatingCount: 63405,
          oneStarRatingCount: 1808,
        },
      },
      highlighted: true,
    },
  ];




 function printBookAuthorsCount(title, ...authors) { // authors can be more than 1 param
    return `The book ${title} has ${authors.length} authors`;
  }

  const { title: book3Title, author: authors } = books[2];

  const bookAuthor = printBookAuthorsCount(
    book3Title,
    'Robert Sedgewick', //rest param 1
    'Kevin Wayne' //rest param 2
  );
  console.log(bookAuthor);
   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);
const testObj = {
  name: 'David',
  movie: 'fight club',
  active: true,
};

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



function getData(...args) {
  args.forEach(({name, movie}) => {
    console.log({name, movie});
  });
}



star

Wed Jul 17 2024 09:44:53 GMT+0000 (Coordinated Universal Time)

#nested #function #foreach #...args #literals
star

Wed Jul 17 2024 09:41:09 GMT+0000 (Coordinated Universal Time)

#nested #function #foreach #...args
star

Fri Jun 21 2024 00:02:46 GMT+0000 (Coordinated Universal Time)

#functions #...args #rest
star

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

#functions #...args #rest
star

Thu Jun 20 2024 05:58:52 GMT+0000 (Coordinated Universal Time)

#functions #...args

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension