Skip to content

Instantly share code, notes, and snippets.

@AhmedHelalAhmed
Last active January 31, 2020 03:24
Show Gist options
  • Select an option

  • Save AhmedHelalAhmed/82cf544b98b316d1f7b83e0ac0446d1c to your computer and use it in GitHub Desktop.

Select an option

Save AhmedHelalAhmed/82cf544b98b316d1f7b83e0ac0446d1c to your computer and use it in GitHub Desktop.
add function to array of objects in js
{
"scripts": [],
"showConsole": true
}
let printName = function() {
console.log(this.name);
};
let items = [
{
name: "ali"
},
{
name: "print"
},
{
name: "abc"
}
];
items.forEach(item => {
item.print = printName;
});
items[2].print();
items[1].print();
items[2].print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment