Home > database >  How to filter an array based on property value, or existence of properties
How to filter an array based on property value, or existence of properties

Time:12-03

How would I go about filtering an array based on specific properties within it?

For example, if I only wanted the objects with the season property in them.

[
  {
    population: 121321313,
    location: 'American city in the East',
  },
  {
    season: 'winter',
    population: 54646546,
    location: 'Canadian city in the East',
  },
  {
    population: 6546467,
    location: 'American city in the West',
  },
  {
    season: 'fall',
    population: 145313,
    location: 'American city in the South',
  },
  {
    population: 12673,
    location: 'Canadian city2 in the East',
  },
  {
    population: 141313,
    location: 'Canadian city in the South',
  },
  {
    season: 'fall',
    population: 1264473,
    location: 'Canadian city4 in the East',
  },
  {
    population: 12673,
    location: 'Canadian city6 in the South',
  },
];

CodePudding user response:

You're looking for Object#hasOwnProperty. Specifially, you're looking to filter your array to only the items which have the property season which you can accomplish like this:

const cities = [
  { "population": 121321313, "location": "American city in the East" },
  { "season": "winter", "population": 54646546, "location": "Canadian city in the East" },
  { "population": 6546467,"location": "American city in the West"},
  { "season": "fall", "population": 145313, "location": "American city in the South" },
  { "population": 12673, "location": "Canadian city2 in the East" },
  { "population": 141313, "location": "Canadian city in the South" },
  { "season": "fall", "population": 1264473, "location": "Canadian city4 in the East" },
  { "population": 12673, "location": "Canadian city6 in the South" }
];

const filteredCities = cities.filter(x => x.hasOwnProperty("season"));

console.dir(filteredCities);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>


You could also use the more modern Reflect.has. See Javascript object.hasOwnProperty() vs Reflect.has() or How do I check if an object has a specific property in JavaScript? for more information.

Show code snippet

const cities = [
  { "population": 121321313, "location": "American city in the East" },
  { "season": "winter", "population": 54646546, "location": "Canadian city in the East" },
  { "population": 6546467,"location": "American city in the West"},
  { "season": "fall", "population": 145313, "location": "American city in the South" },
  { "population": 12673, "location": "Canadian city2 in the East" },
  { "population": 141313, "location": "Canadian city in the South" },
  { "season": "fall", "population": 1264473, "location": "Canadian city4 in the East" },
  { "population": 12673, "location": "Canadian city6 in the South" }
];

const filteredCities = cities.filter(x => Reflect.has(x, "season"));

console.dir(filteredCities);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

you can use statement - for..of, for iterate objects then find your items and push to the new array

const list = [
  {
    "population": 121321313,
    "location": "American city in the East"
  },
  {
    "season": "winter",
    "population": 54646546,
    "location": "Canadian city in the East"
  },
  {
    "population": 6546467,
    "location": "American city in the West"
  },
  {
    "season": "fall",
    "population": 145313,
    "location": "American city in the South"
  },
  {
    "population": 12673,
    "location": "Canadian city2 in the East"
  },
  {
    "population": 141313,
    "location": "Canadian city in the South"
  },
  {
    "season": "fall",
    "population": 1264473,
    "location": "Canadian city4 in the East"
  },
  {
    "population": 12673,
    "location": "Canadian city6 in the South"
  }
  
];

let Newarr = [];

for (const item of list) {

  if(Object.hasOwn(item, "season")) {
      Newarr.push(item);
  }
}

console.log(Newarr);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related