Get callback response

0

How do I get / answer the callback here?

const arr = ["Lucas"];

function minhaCall(sobrenome, indice) {
    return indice + 1 + ") " + sobrenome + " de Carvalho";
}

arr.forEach(minhaCall)

For example, I can not give console.log(arr.forEach(minhaCall))

How would I use the return of this function?

    
asked by anonymous 20.11.2018 / 19:15

2 answers

2

Apparently you want to generate a new array from strings from the original. If this is the case, this is done with Array.map , not Array.forEach .

I recommend reading each one's documentation for more details:

So, I would stay:

const arr = ["Lucas"];

function minhaCall(sobrenome, indice) {
    return indice + 1 + ") " + sobrenome + " de Carvalho";
}

console.log(arr.map(minhaCall));

Or in a simplified way:

const arr = ["Lucas"];
const novoArr = arr.map((nome, i) => '${i+1}) ${nome} de Carvalho');

console.log(novoArr);
    
20.11.2018 / 20:31
-2

Your question was not clear, but I think you're trying to do something like this:

const arr = ['Lucas']

const minhaCall = (sobrenome, indice) => '${indice + 1}) ${sobrenome} de Carvalho'

arr.map((nome, i) => {
    console.log(minhaCall(nome, i))
})
    
20.11.2018 / 19:55