![](/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.