4 回答

TA貢獻1804條經(jīng)驗 獲得超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條經(jīng)驗 獲得超3個贊
您可以使用find來實現(xiàn)您想要做的事情:
const match = branches.find((branch) => {
? return branch.coordinates.find((coords) => {
? ? return finder.longitude === coords.longitude && finder.latitude === coords.latitude
? });
});

TA貢獻1772條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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);
添加回答
舉報