Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

88
Visualizações
Concat results of single function in JavaScript
const example = e => {
        console.log(e);
    };

Each time an API call is made, example() or rather e gives back different strings, such as:

string one string two string three

Is there a way to concat all these strings into one array?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let arr = [];
const example = e => {
  arr.push(e);
  console.log(e);
};
about 4 years ago · Juan Pablo Isaza Relatório

0

Just declare somewhere an array and push the values there.

let arr = [];
const example = item => {
        arr.push(item);
        console.log(arr);
    };
about 4 years ago · Juan Pablo Isaza Relatório

0

First of all I would recommend to use a function that sends your requests, e.g. sendRequest(). If you access a database or some website you have to declare an async function, because it's unsure, when you get your response back. Your sendRequest function can then return a javascript object either if the api call was successful or not. The keyword await waits, until the promise is resolved.

Now to your question: If your call was successful, just push your result to an outer defined array apiResults, thats it.

const apiResults = [];

async function sendRequest() {
  try {
    const apiResult = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
    const apiSuccess = Boolean(apiResult);
    
    if (apiSuccess) {
      apiResults.push(apiResult);
    }
    return { ok: apiSuccess, data: apiResult }; 
  } catch (err) {
    return { ok: false, data: {} };
  }
}

(async function test() {
  console.log('Before', apiResults);
  const response = await sendRequest();
  console.log('After', apiResults);
})();
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda