从对象数组中获取最大值和最小值,并返回与同一对象关联的另一个值,JavaScript

pgky5nke  于 2023-01-04  发布在  Java
关注(0)|答案(2)|浏览(128)

我在处理我正在学习的教育课程中的任务时遇到问题,highestValShoe和lowestValShoe函数返回的结果相同。
我已经尽了最大的努力,但我不明白我在哪里出错了。我真的很感激一些指点。谢谢!

//First I will establish an empty array to push() the shoes in later.
shoeArray = [];

//Now I will create a class for the shoes.
class Shoes {
  constructor(name, productCode, quantity, valuePerItem) {
    this.name = name;
    this.productCode = productCode;
    this.quantity = quantity;
    this.valuePerItem = valuePerItem;
  }

  //This code will enable us to update the quantity.
  updateQuantity(newQuantity) {
    this.quantity = newQuantity;
  }
}

//Now I will create 5 instances for the class.
converse = new Shoes("Converse", 010405, 3, 55.5);
adidas = new Shoes("Adidas", 030602, 5, 85.0);
nike = new Shoes("Nike", 052656, 2, 165.0);
vans = new Shoes("Vans", 745023, 6, 95.5);
fila = new Shoes("Fila", 034567, 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

//This function will enable us to search for any shoe within the array.
function searchShoes(shoeName, shoeArray) {
  for (i = 0; i < shoeArray.length; i++) {
    if (shoeArray[i].name === shoeName) {
      return shoeArray[i];
    }
  }
}

//This function will enable us to search for the lowest value shoe. 
function lowestValShoe (shoeArray) {
  for (i=0; i<shoeArray.length; i++){
    lowestVal = Math.min(shoeArray[i].valuePerItem)
    shoe = shoeArray[i].name
}
  return shoe
}

//This function will enable us to search for the highest value shoe.
function highestValShoe (shoeArray){
  for (i=0; i<shoeArray.length; i++){
    highestVal = Math.max(shoeArray[i].valuePerItem)
    shoe1 = shoeArray[i].name
  }
  return shoe1
}

我尝试返回最大值'Shoe'和最小值'Shoe',当我测试lowestValShoe函数时,我认为它工作正常,但当我将其转换为最大值时,它返回相同的值。
我改变了其中一只鞋的值,使另一只鞋变得最低,然后意识到我的lowestValShoe功能也没有按预期工作。
这两个函数都返回'Fila'

wpx232ag

wpx232ag1#

您需要存储对象,而不是仅存储用于查找最小或最大值鞋的名称。
取第一项,从第二项迭代到最后一项。然后检查value是大于还是小于,并取该项。之后只返回名称。

function highestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i++) {
        if (shoeArray[i].valuePerItem > shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

顺便说一句,请声明所有变量。如果没有,你会得到全局变量,这可能会导致有趣的结果。并使用分号。始终。

//Now I will create a class for the shoes.
class Shoes {
    constructor(name, productCode, quantity, valuePerItem) {
        this.name = name;
        this.productCode = productCode;
        this.quantity = quantity;
        this.valuePerItem = valuePerItem;
      }

    //This code will enable us to update the quantity.
    updateQuantity(newQuantity) {
        this.quantity = newQuantity;
    }
}

//This function will enable us to search for any shoe within the array.
function searchShoes(shoeName, shoeArray) {
    for (let i = 0; i < shoeArray.length; i++) {
        if (shoeArray[i].name === shoeName) return shoeArray[i];
    }
}

//This function will enable us to search for the lowest value shoe. 
function lowestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i++) {
        if (shoeArray[i].valuePerItem < shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

function highestValShoe(shoeArray) {
    let shoe = shoeArray[0];
    for (let i = 1; i < shoeArray.length; i++) {
        if (shoeArray[i].valuePerItem > shoe.valuePerItem) shoe = shoeArray[i];
    }
    return shoe.name;
}

//Now I will create 5 instances for the class.
const
    shoeArray = [],
    converse = new Shoes("Converse", "010405", 3, 55.5),
    adidas = new Shoes("Adidas", "030602", 5, 85.0),
    nike = new Shoes("Nike", "052656", 2, 165.0),
    vans = new Shoes("Vans", "745023", 6, 95.5),
    fila = new Shoes("Fila", "034567", 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

console.log(lowestValShoe(shoeArray));
console.log(highestValShoe(shoeArray));
thtygnil

thtygnil2#

因为你需要重写min和max函数,这样它们就不会只使用Math.min和Math.max,你可以使用Array.reduce把它们变成一行代码:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

function lowestValShoe (shoeArray) {
  return shoeArray.reduce((prev, curr) => (prev.valuePerItem < curr.valuePerItem ? prev : curr));
}

function highestValShoe (shoeArray){
   return shoeArray.reduce((prev, curr) => (prev.valuePerItem > curr.valuePerItem ? prev : curr));   
}

在这里,您可以看到它实时返回FilaNike,分别表示最小值和最大值:

//First I will establish an empty array to push() the shoes in later.
shoeArray = [];

//Now I will create a class for the shoes.
class Shoes {
  constructor(name, productCode, quantity, valuePerItem) {
    this.name = name;
    this.productCode = productCode;
    this.quantity = quantity;
    this.valuePerItem = valuePerItem;
  }

  //This code will enable us to update the quantity.
  updateQuantity(newQuantity) {
    this.quantity = newQuantity;
  }
}

//Now I will create 5 instances for the class.
converse = new Shoes("Converse", 010405, 3, 55.5);
adidas = new Shoes("Adidas", 030602, 5, 85.0);
nike = new Shoes("Nike", 052656, 2, 165.0);
vans = new Shoes("Vans", 745023, 6, 95.5);
fila = new Shoes("Fila", 034567, 3, 45.0);

//This will push all instances into the shoeArray.
shoeArray.push(converse, adidas, nike, vans, fila);

console.log(lowestValShoe(shoeArray));
console.log(highestValShoe(shoeArray));

function lowestValShoe(shoeArray) {
  return shoeArray.reduce((prev, curr) => (prev.valuePerItem < curr.valuePerItem ? prev : curr));
}

function highestValShoe(shoeArray){
   return shoeArray.reduce((prev, curr) => (prev.valuePerItem > curr.valuePerItem ? prev : curr));   
}

相关问题