![](/img/trans.png)
[英]Cannot access data in JSON array nested in JSON object using javascript
[英]How to access a specific element in nested array in JSON data using JavaScript?
我有JSON数据,其结构如下。 意图是查找特定的数据点,例如年利润,即5000。
我想通过按名称查找列(例如“ profit”),确定列索引(在示例中为3),然后使用列索引选择第二个节点中的第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;
根据您提供的JSON结构,以下方法将起作用。 如果您想基于参数获得特定利润,编写函数会很好。
var output = ""
function getProfit(type="annual", column=2) {
var arrForType = yourData.datatable.data.find(arr => arr.indexOf(type) !== -1);
return arrForType[column];
}
var i = yourData.datatable.columns.findIndex(obj => obj.name == "profit");
output += '<p>Annual profit AAPL: ' + getProfit("annual", i) + '</p>';
document.body.innerHTML += output;
您具有2-dimensional
数组,因此,需要两个索引:
const 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"
}
]
}
}
var profitIndex = json.datatable.columns.findIndex(item => item.name == 'profit');
var annualIndex = json.datatable.data.findIndex(array => array.indexOf('annual') > -1);
var annualProfit = json.datatable.data[annualIndex][profitIndex];
如果需要一个功能,它可能如下所示:
var getValueFromJson = function (json, columnName, dataMarker) {
var columnIndex = json.datatable.columns.findIndex(item => item.name == columnName);
var dataMarkerIndex = json.datatable.data.findIndex(array => array.indexOf(dataMarker) > -1);
if (columnIndex < 0 || dataMarkerIndex < 0) {
return null;
}
return json.datatable.data[dataMarkerIndex][columnIndex];
}
console.log(getValueFromJson(json, 'profit', 'quarterly'));
console.log(getValueFromJson(json, 'profit', 'annual'));
console.log(getValueFromJson(json, 'revenue', 'quarterly'));
console.log(getValueFromJson(json, 'revenue', 'annual'));
上面的代码打印:
> 1000
> 5000
> 2000
> 10000
这是基本思想,如果您显然需要扩展,则需要以一种更好的方式进行。
let output = '';
// Searches the desired index (refactor as needed)
const index = spalten.findIndex(obj => obj.name == "profit")
// Extract all the profits (if you dont need all just select the desired one)
daten.map(item => output += `<p>${item[1]} profit ${item[0]}: ${item[index]}</p>`)
您不需要findIndex
只需使用find
和includes
就像这样:
const data = { "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" } ] } }; function findValue(type) { return data.datatable.data.find(e => e.includes(type))[2]; } console.log(findValue("annual")); console.log(findValue("quarterly"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.