我正在嘗試創建數組和/或對象以保存圖形的值。數據以JSON形式出現。每個對象都有一個資產代碼,一個日期和兩個我感興趣的測量值(度量3是兩個網絡的淨值)。我試圖根據資產代碼和數據捕獲日期進行過濾。我想將它編譯成單個對象,我可以循環訪問並按日期提取每個資產的數據。我的嘗試是創建一個數組的數組,但遇到了一些未定義的變量問題。有沒有更簡潔的方法來實現這一目標?JavaScript - 二維數組/對象
var response = []; //to store global variables
response.handle = function handle (resp) {
response.measurment1 = {};
response.measurmentt2 = {};
response.measurement3 = {};
var dateArray = [];
var assetCodeArray = [];
for(object in resp) {
if (extend.contains(dateArray,resp[object].date == false){ // extend.contains is a function that checks if an obj is in an array
dateArray.push(resp[object].date;
}
if (extend.contains(dateArray,resp[object].AssetCode == false){ // extend.contains is a function that checks if an obj is in an array
assetCodeArray.push(resp[object].date;
}
}
for (var code in assetCodeArray) {
response.injObj[assetCodeArray[code]] = [];
response.WthObj[assetCodeArray[code]] = [];
response.netObj[assetCodeArray[code]] = [];
var filteredRespAC = resp.filter(function(y) {
return y.AssetCode == assetCodeArray[code];
});
var AssetCodeInj = 0;
var AssetCodeWth = 0;
var AssetCodeNet = 0;
for (var obj in filteredRespAC) {
for (date in dateArray) {
var filterACDate = filteredRespAC.filter(function(z) {
return z.date == dateArray[date];
});
var volInj = filterACDate[date]["Measurement1"];
var volWth = filterACDate[date]["measurment2"];
var volNet = volInj + volWth;
response.injObj[assetCodeArray[code]].push(volInj);
response.WthObj[assetCodeArray[code]].push(volWth);
response.netObj[assetCodeArray[code]].push(volNet);
}
}
JSON的表示如下:
[
{
"ID" : 12345,
"AssetCode" : "AC1",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12346,
"AssetCode" : "AC1",
"Measure1" : 6789,
"Measure2" : -678,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12347,
"AssetCode" : "AC2",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12348,
"AssetCode" : "AC1",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-13T00:00:00"
}
]
這將是巨大的,如果你可以張貼一個例子JSON對象 – puemos
編輯的職位,包括例如JSON – LCaraway
你JSON是無效的。這是一個複製粘貼錯誤? – Cypher