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。
添加回答
舉報
