How to filter with javascript

Asked

Viewed 92 times

-1

I have an array of objects and I want to filter the properties and return only the properties with the lat and Lon greater than 0

How do I do it? How do I access lat and Lot?

I tried something like that but it doesn’t work:

  var filtered = this.properties;
      this.filtered.filter(p => {
        return p.address.geoLocation.location.lat > 0;
      });

json:

{
properties:[
{
      "usableAreas": 70,
      "listingType": "USED",
      "createdAt": "2017-04-22T18:39:31.138Z",
      "listingStatus": "ACTIVE",
      "id": "7baf2775d4a2",
      "parkingSpaces": 1,
      "updatedAt": "2017-04-22T18:39:31.138Z",
      "owner": false,
      "images": [
        "https://resizedimgs.com/crop/400x300/vr.images.sp/f908948bf1d9e53d36c4734d296feb99.jpg",
        "https://resizedimgs.com/crop/400x300/vr.images.sp/bd37e4c09ddd370529489fbbc461dbec.jpg",
        "https://resizedimgs.com/crop/400x300/vr.images.sp/7b86204f34b751c432c878d607c9d618.jpg",
        "https://resizedimgs.com/crop/400x300/vr.images.sp/3b1142cffc13c49a1e7ea766c20a1d52.jpg",
        "https://resizedimgs.com/crop/400x300/vr.images.sp/f519a27d56696febfb77f6398f4484d8.jpg"
      ],
      "address": {
        "city": "",
        "neighborhood": "",
        "geoLocation": {
          "precision": "NO_GEOCODE",
          "location": {
            "lon": 0,
            "lat": 0
          }
        }
      },
      "bathrooms": 1,
      "bedrooms": 2,
      "pricingInfos": {
        "yearlyIptu": "60",
        "price": "276000",
        "businessType": "SALE",
        "monthlyCondoFee": "0"
      }
    }]

this is the get method:

this.$http
    .get(`properties?_page=${this.page}&_limit=${this.limit}`)
    .then(res => {
      this.properties = this.properties.concat(res.data);
      this.page++;

      if (res.data.length === 0) this.loadMore = false;
      console.log(this.properties);
    });

1 answer

5


You can do it like this:

this.filtered = this.properties.filter(p => {
  const {lat, lon} = p.address.geoLocation.location;
  return Math.min(lat, lon) > 0;
});

The function .filter returns a new array with the values that pass the return sends. Thus, the Math.min returns the minimum value of the two and therefore being the smallest > 0 you have the condition resolved.

Example:

const properties = [{
    "id": "A",
    "address": {
      "city": "",
      "neighborhood": "",
      "geoLocation": {
        "precision": "NO_GEOCODE",
        "location": {
          "lon": 0,
          "lat": 1000
        }
      }
    }
  },
  {
    "id": "B",
    "address": {
      "city": "",
      "neighborhood": "",
      "geoLocation": {
        "precision": "NO_GEOCODE",
        "location": {
          "lon": 100,
          "lat": 100
        }
      }
    }
  },
  {
    "id": "C",
    "address": {
      "city": "",
      "neighborhood": "",
      "geoLocation": {
        "precision": "NO_GEOCODE",
        "location": {
          "lon": 100,
          "lat": 0
        }
      }
    }
  }
];

const filtered = properties.filter(p => {
  const {lat, lon} = p.address.geoLocation.location;
  return Math.min(lat, lon) > 0;
});

console.log(JSON.stringify(filtered));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.