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

237
Views
How to extract different parts of JSON to get a specific array of objects?

I'd like to filter parts of a given JSON array to produce a specific array of objects.

My JSON is like this:

capitals = {"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Point","coordinates":[12.453386544971766,41.903282179960115]},"properties":{"name":"Vatican City","countryName":"Vatican (Holy Sea)"}},
{"type":"Feature","geometry":{"type":"Point","coordinates":[12.441770157800141,43.936095834768004]},"properties":{"name":"San Marino","countryName":"San Marino"}},
{"type":"Feature","geometry":{"type":"Point","coordinates":[9.516669472907267,47.13372377429357]},"properties":{"name":"Vaduz","countryName":"Liechtenstein"}},
...
{"type":"Feature","geometry":{"type":"Point","coordinates":[103.85387481909902,1.2949793251059418]},"properties":{"name":"Singapore","countryName":"Singapore"}}
]}

I need to extract 10 random coordinates and assign first 5 to a specific key and last 5 to a different key in an array of 5 objects looking like this:

[
{startLat: c1, startLng: c2, endLat: c3, endLng: c4, labelLat: c3, labelLng: c4}
{startLat: c5, startLng: c6, endLat: c7, endLng: c8, labelLat: c7, labelLng: c8}
{startLat: c9, startLng: c10, endLat: c11, endLng: c12, labelLat: c11, labelLng: c12}
{startLat: c13, startLng: c14, endLat: c15, endLng: c16, labelLat: c15, labelLng: c16}
{startLat: c17, startLng: c18, endLat: c19, endLng: c20, labelLat: c19, labelLng: c20}
]

I got to a point where I have two separate arrays with correct values but I'm not sure how to merge them correctly into a single one. Maybe there's a better/shorter method to achieve the result?

const   data = capitals.features,
        random_sample = (obj, n) => d3.shuffle(obj.slice()).slice(0, n);
    
let     c10 = random_sample(data, 10),
        c5a = c10.slice(0, 5).map((d) => ({
                startLat: d.geometry.coordinates[1],
                startLng: d.geometry.coordinates[0]
        })),
        c5b = c10.slice(5, cap10.length).map((d) => ({
            endLat: d.geometry.coordinates[1],
            endLng: d.geometry.coordinates[0],
            labelLat: d.geometry.coordinates[1],
            labelLng: d.geometry.coordinates[0]
        }));
about 4 years ago · Juan Pablo Isaza
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!