3 回答

TA贡献2041条经验 获得超4个赞
我认为你只需要这样做,检查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)

TA贡献1820条经验 获得超10个赞
您可以根据坐标值进行过滤,如下所示。
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);

TA贡献1789条经验 获得超8个赞
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。
添加回答
举报