Je sais que si j'ai par exemple un tableau comme celui-ci je pourrais filtrer par nom (ou id): p>
3 Réponses :
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);
this will return a new array location without the location 3 in it.
Je pense que vous avez juste besoin de faire cela, vérifiez p> coordonnées code>. Vous n'avez pas besoin d'appuyer sur un autre tableau car Filtre code> renvoie un nouveau tableau de toute façon. 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)
Vous pouvez filtrer en fonction de la valeur CORDINATES comme ci-dessous.
p>
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);
Dans votre fonction de filtrage:
lieu de retour.HasownProperty ("coordonnées") code>