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

134
Visualizações
Not all data is displayed in an array

I'm trying to get data via api.

apiClient:

export interface apiRequest {
  locations: LocationOptions[];

}
interface FOLocations {
  locationId: number;
}

interface LocationResult {
  location: FOLocations[];
  cityName: string;
}

export const locationCheck = async (
  apiKey: string,
  payload: apiRequest
): Promise<LocationResult[]> => {
  let response = await axios.post<LocationResult[]>(
    `api...`,
    payload
  );
  return response.data;

runCode:

  const locationPayload : apiRequest = {
      locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
    };
    const locationResponse = await locationCheck(apiKey,locationPayload);
    const [locationResponseRes] =  locationResponse;
    console.log(locationResponseRes);

Actual output :

{
  "location": {
    "locationId": 1,
    "cityName: "London",
  }
}

Expected output(that what i got in Postman) :

[
    {
        "location": {
            "locationId": 1,
            "cityName: "London",
        }
    },
    {
        "location": {
            "locationId": 2,
            "cityName": "New York",
        },    
},
]

So, as you can see, that not all data is displayed in an array. Maybe i need to do it via entity[] ?

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

0

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. so instead of unpacking the first object from an array, just return the array itself.

I've added a small example for you to better understand how destructuring works:

[a, b] = [10, 20];

console.log(a); // 10

[first, second] = [10, 20, 30, 40, 50];

console.log(second); // 20

[first, second, ...rest] = [10, 20, 30, 40, 50];

console.log(rest); // [30, 40, 50]

Bottom line, return locationResponse instead of the locationResponseRes object (that line can be removed)

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