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

239
Views
¿Cómo extraer diferentes partes de JSON para obtener una matriz específica de objetos?

Me gustaría filtrar partes de una matriz JSON dada para producir una matriz específica de objetos.

Mi JSON es así:

 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"}} ]}

Necesito extraer 10 coordenadas aleatorias y asignar las primeras 5 a una clave específica y las últimas 5 a una clave diferente en una matriz de 5 objetos con este aspecto:

 [ {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} ]

Llegué a un punto en el que tengo dos matrices separadas con valores correctos, pero no estoy seguro de cómo combinarlas correctamente en una sola. ¿Quizás hay un método mejor/más corto para lograr el resultado?

 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!