简体   繁体   English

JS:具有多个条件的循环

[英]JS: Loop with multiple conditions

I have an array of objects (cars), from which I want to show just the cars which "compete".我有一系列对象(汽车),我想从中只展示“竞争”的汽车。

const cars = [
  {
    name: "red",
    competes: true,
    category: 1
  },
  {
   name: "blue",
   competes: false,
   category: 1
  },
  {
    name: "green",
    competes: true,
     category: 2
  },
  {
    name: "yellow",
    competes: true,
    category: 3
  }
]

But from those cars which compete, i only want to show those cars which are in category one, which is fine with a for loop.但是从那些竞争的汽车中,我只想展示那些属于第一类的汽车,这可以使用 for 循环。

The objects are now static but since i want to change them in a second moment, i need a code which checks if the cars compete and then checks if there are cars in "category" one.这些对象现在是 static 但由于我想在第二个时刻更改它们,我需要一个代码来检查汽车是否竞争,然后检查是否有汽车属于“类别”之一。

  • If there are cars in "category 1", list them如果有“类别 1”中的汽车,请列出它们
  • If there are no cars in "category" 1, list those in "category" 2 (but not 3)如果“类别”1 中没有汽车,请列出“类别”2(但不是 3)中的汽车
  • And so on等等

My try was with a loop inside a loop, bit that doesnt work since it displays all multiple times我的尝试是在一个循环中使用一个循环,这不起作用,因为它显示所有多次

for (let i = 0; i < cars.length; i++) {
  if (cars[i].competes === false) continue;
  
  for (let f = 0; f < cars.length; f++) {
  if (cars[f].category > 1) break;
  console.log(`Cat1: ${cars[f].name}`);
}
}

How can this be resolved if the "category" attributes inside "cars" are for example all 2 or more?如果“汽车”中的“类别”属性例如都是 2 个或更多,如何解决这个问题?

const cars = [
  {
    name: "red",
    competes: true,
    category: 2
  },
  {
   name: "blue",
   competes: false,
   category: 2
  },
  {
    name: "green",
    competes: true,
     category: 3
  },
  {
    name: "yellow",
    competes: true,
    category: 4
  }
]

Here is a simple solution with sort and filter这是一个带有排序和过滤的简单解决方案

 const cars = [ { name: "red", competes: true, category: 1 }, { name: "blue", competes: false, category: 1 }, { name: "green", competes: true, category: 2 }, { name: "yellow", competes: true, category: 3 } ] let result = cars.sort((a,b) => a.category - b.category).filter(car => car.competes && (cars.length && car.category == cars[0].category)) console.log(result)

We can start by getting the 'competing category' by getting the lowest competing category present, using Math.min() and Array.filter()我们可以通过使用Math.min()和 Array.filter() 获得最低的竞争类别来获得“竞争类别”

We can then get all competing cars by using Array.filter() to return only cars with a competes value that is truthy and also belongs to the competing category.然后,我们可以通过使用Array.filter()仅返回具有真实值且也属于竞争类别的competes值的汽车来获取所有竞争汽车。

 const cars = [ { name: "red", competes: true, category: 1 }, { name: "blue", competes: false, category: 1 }, { name: "green", competes: true, category: 2 }, { name: "yellow", competes: true, category: 3 } ] // Only this category is competing.. const competingCategory = Math.min(...cars.filter(car => car.competes).map(({category}) => category)); const competingCars = cars.filter(car => car.competes && car.category === competingCategory); console.log('Competing cars:', competingCars)
 .as-console-wrapper { max-height: 100%;important: top; 0; }

This example has only category 2 and 3 present:此示例仅存在类别 2 和 3:

 const cars = [ { name: "blue", competes: true, category: 2 }, { name: "green", competes: true, category: 2 }, { name: "yellow", competes: true, category: 3 } ] // Only this category is competing.. const competingCategory = Math.min(...cars.filter(car => car.competes).map(({category}) => category)); const competingCars = cars.filter(car => car.competes && car.category === competingCategory); console.log('Competing cars (example II):', competingCars)
 .as-console-wrapper { max-height: 100%;important: top; 0; }

First of all you sort the array based on the category and then filter out an array with cars that are gonna compete首先,您根据类别对数组进行排序,然后过滤出一个包含要竞争的汽车的数组

 const cars = [ { name: "red", competes: true, category: 1 }, { name: "blue", competes: false, category: 1 }, { name: "green", competes: true, category: 2 }, { name: "yellow", competes: true, category: 3 } ] const sorted = cars.sort((a, b) => a.category - b.category); const compete = sorted.filter(x => x.competes && x.category == sorted[0].category); console.log(compete[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM