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]
}));