Reputation: 103
I have this Data Structure :
const data = [
{ id: 1, urlName: 'balance-at-work', offices: [ { location: 'Sydney, Australia', in_range: false }, ], }, { id: 2, urlName: 'spring-development', offices: [ { location: 'Banbury, Oxfordshire', in_range: true }, ], }, { id: 3, urlName: 'talent-lab', offices: [ { location: 'México City, Mexico', in_range: false }, { location: 'London, UK', in_range: true }, ], }, ];
I want to reduce the offices array inside each object by using the great circle distance formula.
So far i have been able to calculate the great circle distance and add a dist
key inside each office object. What i am having issue with, is a clean way to remove all the objects inside each offices array for each user when dist
is greater than a given range.
Upvotes: 0
Views: 77
Reputation: 3371
A simple way to do this might be something like the following (is_office_in_range
is just a placeholder for however you determine whether an office should be included or not):
const data = [
{
id: 1,
urlName: 'balance-at-work',
offices: [
{
location: 'Sydney, Australia',
},
],
},
{
id: 2,
urlName: 'spring-development',
offices: [
{
location: 'Banbury, Oxfordshire, UK',
},
],
},
{
id: 3,
urlName: 'talent-lab',
offices: [
{
location: 'México City, Mexico',
},
{
location: 'London, UK',
},
],
},
];
const is_office_in_range = (office) => office.location.endsWith('UK');
const res = data
.map((company) => ({
...company,
offices: company.offices.filter((office) => is_office_in_range(office))
}))
.filter((company) => company.offices.length);
console.log(res);
Upvotes: 1