【发布时间】:2019-02-25 00:24:53
【问题描述】:
我有 JSON 数据,其结构如下。意图是查找特定的数据点,例如年利润,即5000。
我想通过按名称查找列来做到这一点,例如“profit”,标识列索引(示例中为 3),然后使用列索引选择“data”数组的第二个节点(“annual”)中的第 n(3)个元素。
如何使用 Javascript 中的 findIndex() 函数执行此操作(请参阅下面我的代码的关键部分)?
JSON 数据:
{
"datatable": {
"data": [
[
"AAPL",
"quarterly",
1000,
2000
],
[
"AAPL",
"annual",
5000,
10000
]
],
"columns": [{
"name": "ticker"
"type": "String"
},
{
"name": "timedim"
"type": "String"
},
{
"name": "profit",
"type": "Integer"
},
{
"name": "revenue",
"type": "Integer"
}
]
}
}
JavaScript 代码:
// daten contains the "data" array of the JSON dataset
// spalten contains the "columns" array of the JSON dataset
var i = spalten.findIndex(obj => obj.name == "profit");
output += '<p>Annual profit AAPL: ' + daten[i] + '</p>';
elroot.innerHTML += output;
【问题讨论】:
标签: javascript arrays json object ecmascript-6