-
缥缈止盈
我认为你只需要这样做,检查coordinates。您不需要推入另一个数组,因为filter无论如何都会返回一个新数组。 var locations = [ { name: 'location 1', id: '1', coordinates: {long: '', lat: ''} }, { name: 'location 2', id: '2', coordinates: {long: '', lat:''} }, { name: 'location 3', id: '3', },];var res = locations.filter((location) => location.coordinates);console.log(res)
-
拉莫斯之舞
您可以根据坐标值进行过滤,如下所示。let locations = [{name: 'location 1', id: '1', coordinates: {long: '', lat: ''} }, { name: 'location 2', id: '2', coordinates: {long: '', lat:''} } ];let coordinates = locations.filter(l => !!l.coordinates);console.log(coordinates);
-
拉丁的传说
const locations = [ { name: 'location 1', id: '1', coordinates: {long: 1, lat: 1} }, { name: 'location 2', id: '2', coordinates: {long: 2, lat: 2} }, { name: 'location 3', id: '3', },];const filterLocation = (locations) => { let filteredLocations = [] locations.filter((location) => { if(location.hasOwnProperty("coordinates")) { filteredLocations.push(location) } }) return filteredLocations}const newLocations = filterLocation(locations);console.log('newLocations', newLocations);这将返回一个新的数组位置,其中没有位置 3。