4 回答
TA贡献1804条经验 获得超8个赞
这将找到坐标等于的分支finder
var branches = [
{
locationName: "Cool Towers",
coordinates: [
{
latitude: 25.33853805869942,
longitude: 55.393801012135796,
},
{
latitude: 25.33848836326937,
longitude: 55.393874772883706,
},
{
latitude: 25.338230189121408,
longitude: 55.39362935075884,
}
],
},
{
locationName:"Great Towers",
coordinates: [
{
latitude: 25.16626719853835,
longitude: 55.26170584184425,
},
{
latitude: 25.166607063076132,
longitude: 55.26206257564323,
}
],
}
];
var finder = {
latitude: 25.166607063076132,
longitude: 55.26206257564323,
}
var branch = branches.find(branch => {
return branch.coordinates.find(coordinate => {
return coordinate.latitude == finder.latitude &&
coordinate.longitude == finder.longitude
})
});
console.log(branch.locationName)
TA贡献1851条经验 获得超3个赞
您可以使用find来实现您想要做的事情:
const match = branches.find((branch) => {
return branch.coordinates.find((coords) => {
return finder.longitude === coords.longitude && finder.latitude === coords.latitude
});
});
TA贡献1772条经验 获得超8个赞
您可以使用Array.find和Array.findIndex来完成
const branches = [
{
locationName: "Cool Towers",
coordinates: [
{
latitude: 25.33853805869942,
longitude: 55.393801012135796,
},
{
latitude: 25.33848836326937,
longitude: 55.393874772883706,
},
{
latitude: 25.338230189121408,
longitude: 55.39362935075884,
}
],
},
{
locationName:"Great Towers",
coordinates: [
{
latitude: 25.16626719853835,
longitude: 55.26170584184425,
},
{
latitude: 25.166607063076132,
longitude: 55.26206257564323,
}
],
}
];
const finder = {
latitude: 25.166607063076132,
longitude: 55.26206257564323
};
const findCoordinate = (co) => {
return (co.latitude === finder.latitude) && (co.longitude === finder.longitude);
}
const branch = branches.find((item) => item.coordinates.findIndex(findCoordinate) >= 0);
if (branch) {
console.log(branch.locationName);
} else {
// Not found
}
TA贡献1806条经验 获得超5个赞
let result;
for(let i = 0; i < branches.length; i++) {
if (branches[i].coordinates.findIndex(coord => coord.latitude === finder.latitude && coord.longitude === finder.longitude) >= 0 ) {
result = branches[i].locationName;
break;
}
}
console.log(result);
添加回答
举报