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

123
Visualizações
How would I make this output a smaller more neat string?

I have been messing around with googles APIs a little bit, and I am trying to create a in terminal command that tells me how far away I am from something and how long it would take to get there. The issue that I am having is that when I run:

var axios = require('axios');

var config = {
    method: 'get',
    url: 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Washington%2C%20DC&destinations=New%20York%20City%2C%20NY&units=imperial&key=AIzaSyCbwuhNvOJQrYWnLRF6WjzJeOcnhYYfpZA',
    headers: {}
};

axios(config)
    .then(function(response) {
        console.log(JSON.stringify(response.data.rows));
    })
    .catch(function(error) {
        console.log(error);
    }); 

Right now it is outputting:

[{"elements":[{"distance":{"text":"225 mi","value":361918},"duration":{"text":"3 hours 52 mins","value":13938},"status":"OK"}]}]

And I would LIKE the output format to display it like:

you are 225 miles or 3 hours 52  mins away

How would I go about making the output look like the second supplied example?

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

0

response.data.rows is a JSON object. JSON.stringify() converts that into a string, which you don't want - you want the object.

If you don't stringify it, you can access the distance string with response.data.rows[0].elements[0].distance.text, and the time with response.data.rows[0].elements[0].duration.text

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this, but I'm assuming you are receiving data in this order

I would suggest you to add null checks as well.

let data = [{
  "elements": [{
    "distance": {
      "text": "225 mi",
      "value": 361918
    },
    "duration": {
      "text": "3 hours 52 mins",
      "value": 13938
    },
    "status": "OK"
  }]
}];

let firstElement = data[0].elements[0];

let constructedString = `You are ${firstElement.distance.text} or ${firstElement.duration.text} away`;

console.log(constructedString);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use the following one-liner:

var data = [{"elements":[{ "distance": {"text": "225 mi", "value": 361918}, "duration": {"text": "3 hours 52 mins", "value": 13938}, "status": "OK"}]}];
console.log(`you are ${ data[0].elements[0].distance.text} or ${data[0].elements[0].duration.text} away`);

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