Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
How to filter an array based on property value, or existence of properties

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',
  },
];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);


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.

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda