【问题标题】:How to filter an array based on property value, or existence of properties [duplicate]如何根据属性值或属性的存在过滤数组[重复]
【发布时间】:2023-04-03 08:05:02
【问题描述】:

我将如何根据其中的特定属性过滤数组?

例如,如果我只想要其中包含 season 属性的对象。

[
  {
    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',
  },
];

【问题讨论】:

标签: javascript arrays filter


【解决方案1】:

你可以使用 语句 - for..of, for iterate objects 然后找到你的项目并推送到新数组

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

【讨论】:

  • hasOwn 目前的compatibility 非常不完整,您应该只在警告的情况下推荐它。有关对象属性检查的更完整说明,请参阅@DM 的答案。
  • 首先,感谢您的指导,但我认为一般来说值得使用浏览器兼容性我尝试通过 chrome good working @pilchard
  • 您尝试了一个更新的浏览器,所以没问题?兼容性表的存在是有原因的,并不是每个人都在运行最新的 Chrome。 caniuse
  • 我没有违反你的话,但如果我们想这样想,不要使用tailwind css,因为它根本不适用于旧版本的chrome @pilchard
【解决方案2】:

您正在寻找Object#hasOwnProperty。具体来说,您正在寻找 filter 您的数组,仅查找具有 season 属性的项目,您可以像这样完成:

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

您也可以使用更现代的Reflect.has。请参阅Javascript object.hasOwnProperty() vs Reflect.has()How do I check if an object has a specific property in JavaScript? 了解更多信息。

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

【讨论】:

猜你喜欢
  • 2015-10-13
  • 2010-10-06
  • 2016-05-15
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多