Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!