英文:
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: 'Apple, Inc',
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论