I want the array.forEach to
for each of its elements, console log
the entire array once, logging array.length
times in total
instead of just console log
each of its 10 elements.
To accomplish this, I need to have a reference to the array inside the array.forEach
(callback) function:
array.forEach(
(element) => {
console.log(the entire array, not the element)
} );
Or this example:
const callbackMale = () => {
let name = this.name;
return 'Mr. ' + name;
}
const callbackFemale = () => {
let name = this.name;
return 'Ms. ' + name;
}
//I need to use the 'name' property value of the object which a method using this callback is called upon
// i need this.name === object.name for the callback to work
};
const obj 1= {
name: 'Foo';
methodA(callback){
console.log(callback());
}
}
const obj2= {
name: 'Bar';
methodB(callback){
console.log(callback());
}
}
obj1.methodA(callBackMale); // Mr. Foo
obj2.methodB(callBackFemale); //Ms. Bar
(OR in general, let the callback do something to/with the obj/arr the method is called upon)