What would be the most efficient way to return multiple things in a .map function. For example, 2 separate console.log statements?
For example: This works just fine:
return (
<div className="App">
{mycataobjects.map((myobject) => console.log(myobject.name))}
</div>
);
However, how would I add another console.log to this map, so that for each object in the array I get the phrase "Hello"
I tried the below but it does not work. It only prints "Hello" once. I want it printed for every object in the my.object array. What would be the best way to do this?
return (
<div className="App">
{mycataobjects.map(
(myobject) => console.log(myobject.name),
console.log("Hello")
)}
</div>
);
If you are really interested in the return values you could return a tuple kind of type and then flatten the array:
const arr = mycataobjects.map(item => [1, 2]);
const values = arr.flat();
However in your example if you just want to call console.log twice you'd need to add braces to your arrow function body:
mycataobjects.map(
(myobject) => {
console.log(myobject.name);
console.log("Hello");
}
);
Note that console.log is a void function so would just return undefined. Trying to grab its return value is not all that useful here.