在嵌套结构中使用 Math.max 的 JavaScript 示例:

huangapple go评论58阅读模式
英文:

Using Math.max in Nested Structure JavaScript

问题

我尝试访问公司在嵌套结构中获得的最大利润,但无法让Math.max()来完成。我尝试使用let,还尝试将数字复制粘贴到语句中,但都不起作用。我以为通过使用Math.max()并在括号内插入到利润的路径,它会起作用。有人可以解释给我如何做或我在做什么错吗。控制台一直显示NaN或属性未定义。请参见下面的代码:

let company = {
  name: 'Apple, Inc',
  founded: 1976,
  financials: {
    incomeStatement: {
      years: [2020, 2019, 2018],
      revenue: [125, 120, 115],
      costs: [100, 100, 100],
      profit: [25, 20, 15]
    },
    balanceSheet: {
      years: [2020, 2019, 2018],
      assets: [200, 190, 180],
      liabilties: [100, 95, 90],
      equity: [100, 95, 90]
    },
    cashFlow: {
      years: [2020, 2019, 2018],
      operating: [75, 65, 55],
      investing: [22, 20, 18],
      financing: [-94, -80, -75]    
    }
  },
  competitors: ['Microsoft', 'Amazon', 'Samsung']
}

console.log(company.name);
console.log(company.competitors);
console.log(Math.max(...company.financials.incomeStatement.profit));
console.log(company.competitors[2]);
console.log(company.financials.incomeStatement.years);
console.log(company.financials.incomeStatement.revenue[0]);
英文:

I am trying to access the maximum profit made by this company in a nested structure and I cannot get Math.max() to do it for me. I have tried using let and also just copying and pasting the numbers into a statement and it will not work. I thought by using Math.max() and inserting the path to the profit within the parentheses it would do so. Can anyone explain to me how to do this or what I am doing wrong. See code below

let company = {
  name: 'Apple, Inc',
  founded: 1976,
  financials: {
    incomeStatement: {
      years: [2020, 2019, 2018],
      revenue: [125, 120, 115],
      costs: [100, 100, 100],
      profit: [25, 20, 15]
    },
    balanceSheet: {
      years: [2020, 2019, 2018],
      assets: [200, 190, 180],
      liabilties: [100, 95, 90],
      equity: [100, 95, 90]
    },
    cashFlow: {
      years: [2020, 2019, 2018],
      operating: [75, 65, 55],
      investing: [22, 20, 18],
      financing: [-94, -80, -75]    
    }
  },
  competitors: ['Microsoft', 'Amazon', 'Samsung']
}

console.log(company.name);
console.log(company.competitors);
console.log(Math.min(company.financials.incomeStatement.profit));
console.log(company.competitors[2]);
console.log(company.financials.incomeStatement.years);
console.log(company.financials.incomeStatement.revenue[0]);

I am trying to access the maximum profit made by this company in a nested structure and I cannot get Math.max() to do it for me. I have tried using let and also just copying and pasting the numbers into a statement and it will not work. I thought by using Math.max() and inserting the path to the profit within the parentheses it would do so. Can anyone explain to me how to do this or what I am doing wrong. The console keeps saying NaN or either property not defined. See code below

答案1

得分: 1

要获得现有数组的最大值,您可以使用 Math.max.apply()

let maxProfit = Math.max.apply(null, company.financials.incomeStatement.profit);
console.log(maxProfit);

这是一个可运行的示例:

英文:

To get the maximum value from an existing array you can use Math.max.apply():

let maxProfit = Math.max.apply(null, company.financials.incomeStatement.profit);
console.log(maxProfit);

Here's a working example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let company = {
  name: &#39;Apple, Inc&#39;,
  founded: 1976,
  financials: {
    incomeStatement: {
      years: [2020, 2019, 2018],
      revenue: [125, 120, 115],
      costs: [100, 100, 100],
      profit: [25, 20, 15]
    }
    // other data omitted for brevity...
  }
}

let maxProfit = Math.max.apply(null, company.financials.incomeStatement.profit);
console.log(maxProfit);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月11日 21:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定