(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>`; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter