如何过滤和查找对象内部

我想在 branches 数组中搜索 finder 对象,如果找到匹配项,那么我想获取 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,

  }


本案例预期结果为:Great Towers


这就是我尝试过的。但这总是给我第一个位置名称


const filter = Object.entries(finder),

  result = branches

    .filter(({ coordinates }) =>

      filt.every(([key, value]) => coordinates[key] === value)

    )

    .map(({ locationName }) => locationName);


潇湘沐
浏览 143回答 4
4回答

胡说叔叔

这将找到坐标等于的分支findervar 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)

皈依舞

您可以使用find来实现您想要做的事情:const match = branches.find((branch) => {  return branch.coordinates.find((coords) => {    return finder.longitude === coords.longitude && finder.latitude === coords.latitude  });});

料青山看我应如是

您可以使用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}

忽然笑

let result;for(let i = 0; i < branches.length; i++) {&nbsp; &nbsp; if (branches[i].coordinates.findIndex(coord => coord.latitude === finder.latitude && coord.longitude === finder.longitude) >= 0 ) {&nbsp; &nbsp; &nbsp; result = branches[i].locationName;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp;&nbsp;}console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript