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

131
Visualizações
convert promise JSON return in a array of objects

My promise returns a JSON like this:

‘[ {"amazing data": "hello", "amazing again": "yep"}, {"amazing data": "hello", "amazing again": "yep"}]’

I would just like to return an array of objects:

[
 {"amazing data": "hello", "amazing again": "yep"},
 {"amazing data": "hello", "amazing again": "yep"}
]

I would like to access it this way because I can work with reduce after the promise returns.

My code:

const amazingPromise = await getData().then((data) => {
   return data
})


const prodArray = Array.from(amazingPromise.reduce((acc, o) => {
    Object.entries(o)
      .forEach(([k, v]) => {
        if(!acc.has(k)) acc.set(k, [k])
        
        acc.get(k).push(Array.isArray(v) ? v.join(' | ') : v)
      })
    
    return acc
  }, new Map()).values())

Any help will be great. Thanks.

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

0

If your project is a Web project, you can use react.State() for solve this. For example, in my projects, I use the state to save the results of an API request like your example.

const App = ()=>{
    const[array, setArray] = useState([]); //Init the array as a empty array
    
    useEffect( ()=>{
      const RequestAPI = async ()=>{
           const URL = "https://...";
           const response = await fetch(URL);
           const newArray = await response.json();
           setArray(newArray);
      };
      RequestAPI();
     },[]);
     
    return (
      {array.map(item => <ComponentItem /> ) }
}

Try to use something similar to state and async functions in your project as its possible

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