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

146
Visualizações
convert a json response object to array of arrays

I have a collection of locations in mongodb, I wrote a simple api to get the locations, following is the code:

app.get('/onlyfields', (req, res) => {
   
      const promise1 = onlyfields.find({}, {_id:0})
      const promise2 = geopoints.find({}, {locations:1, _id:0})
      Promise.all([promise1, promise2])
        .then(([result1, result2]) => {
          res.send({ fields: result1, rows: result2 })
          console.log(result2)
        })
        .catch(err => {
          return res.status(400).json({
            error: 'Erorr en STATUS2'
          })
        })
    
})


The locations data is stored in result2.

the console.log shows the following response:


MongoDB Connected
[
  {
    locations: [
      2,
      '2015-01-15 14:00:41 +00:00',
      '2015-01-15 14:11:18 +00:00',
      1,
      1.4,
      -73.9837265,
      40.74634171,
      -73.96679688,
      40.76140594,
      8.5,
      0,
      9.3
    ]
  },
  {
    locations: [
      2,
      '2015-01-15 14:00:41 +00:00',
      '2015-01-15 14:11:18 +00:00',
      1,
      1.4,
      -73.9837265,
      40.74634171,
      -73.96679688,
      40.76140594,
      8.5,
      0,
      9.3
    ]
  }
]

I need to modify the result2 data to the following format, whaich is an array of arrays:

[
   [
      2,
      '2015-01-15 14:00:41 +00:00',
      '2015-01-15 14:11:18 +00:00',
      1,
      1.4,
      -73.9837265,
      40.74634171,
      -73.96679688,
      40.76140594,
      8.5,
      0,
      9.3
    ]
  ,
   [
      2,
      '2015-01-15 14:00:41 +00:00',
      '2015-01-15 14:11:18 +00:00',
      1,
      1.4,
      -73.9837265,
      40.74634171,
      -73.96679688,
      40.76140594,
      8.5,
      0,
      9.3
    ]

]

Would appreciate any help. Thanks in advance.

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

0

You can simply map each of the result's location array, e.g.

function flatten(data) {
    return data.map(currEntry => currEntry.locations)
}

const data = [
    {
        locations: [
            2,
            '2015-01-15 14:00:41 +00:00',
            '2015-01-15 14:11:18 +00:00',
            1,
            1.4,
            -73.9837265,
            40.74634171,
            -73.96679688,
            40.76140594,
            8.5,
            0,
            9.3
        ]
    },
    {
        locations: [
            2,
            '2015-01-15 14:00:41 +00:00',
            '2015-01-15 14:11:18 +00:00',
            1,
            1.4,
            -73.9837265,
            40.74634171,
            -73.96679688,
            40.76140594,
            8.5,
            0,
            9.3
        ]
    }
];

function flatten(data) {
    return data.map(currEntry => currEntry.locations)
}

console.log(flatten(data))

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