javascript - 如何访问值以使用 JavaScript 中嵌套对象的值?
问题描述
我被困住了我使用金融 API,但是我无法理解如何获取该信息以在页面上呈现。我需要在配置文件和 vol Avg 中获得价格。请帮忙。
这就是我使用的,我能够console log data.body
返回嵌套对象。
data {
symbol: 'AAPL',
profile: {
price: 291.45,
beta: '1.139593',
volAvg: '36724977',
mktCap: '1345844630130.00',
lastDiv: '2.92',
range: '142-233.47',
changes: -0.75,
changesPercentage: '(-0.25%)',
companyName: 'Apple Inc.',
exchange: 'Nasdaq Global Select',
industry: 'Computer Hardware',
website: 'http://www.apple.com',
description: 'Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.',
ceo: 'Timothy D. Cook',
sector: 'Technology',
image: 'https://financialmodelingprep.com/images-New-jpg/AAPL.jpg'
}
}
app.post('/search', (req, res) => {
superagent.get(`https://financialmodelingprep.com/api/v3/company/profile/AAPL`).then(data => {
const companyData = data.body.map(company => new Company(company));
// console.log(data, companyData);
res.render('/search', { companyData });
}).catch(error => {
res.render('error', { error });
});
});
解决方案
您可以访问价格data.profile.price
和 avgvoldata.profile.volAvg
不要忘记添加它包含的内容,在这种情况下我猜是companyData
companyData.data.profile.price
推荐阅读
- php - 如果数据库中的值等于字符串,则将数组添加到嵌套 JSON
- python - 如何将分类时间序列数据输入 LSTM
- kotlin - Kotlin 中用作度量单位的内联类的常用方法和运算符
- c# - 快速操作:字段类生成器
- ruby-on-rails - Rails 嵌套查询
- python - Django 等价于 SQLAlchemy ORM 事件
- android - gms:play-services-ads:17.1.1 给出错误所有 com.google.android.gms 库必须使用完全相同的版本规范
- javascript - 如何使 Vue.js 中的值成为非响应式的?
- python - 无法找到两列不同熊猫数据集之间的相关性
- mysql - MySQL 行是否有全局启用/禁用?