2016-01-15 169 views
0

我正在嘗試創建數組和/或對象以保存圖形的值。數據以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" 
    } 
] 
+0

這將是巨大的,如果你可以張貼一個例子JSON對象 – puemos

+0

編輯的職位,包括例如JSON – LCaraway

+0

你JSON是無效的。這是一個複製粘貼錯誤? – Cypher

回答

1

我認爲這至少是你想要的一部分。

data.reduce(function (last, current, index, array) { 
    if (last[current.AssetCode] == undefined) { 
    console.log(last); 
    last[current.AssetCode] = array.filter(function (value) { 
     return current.AssetCode === value.AssetCode; 
    }); 
    return last; 
    } 
    return last; 
}, {}); 

此函數將您的JSON(固定花括號後)轉換爲此。

{ 
    AC1: [{ 
    AssetCode: "AC1", 
    Date: "2016-01-14T00:00:00", 
    ID: 12345, 
    Measure1: 12345, 
    Measure2: -1234 
}, { 
    AssetCode: "AC1", 
    Date: "2016-01-14T00:00:00", 
    ID: 12346, 
    Measure1: 6789, 
    Measure2: -678 
}, { 
    AssetCode: "AC1", 
    Date: "2016-01-13T00:00:00", 
    ID: 12348, 
    Measure1: 12345, 
    Measure2: -1234 
}], 
    AC2: [{ 
    AssetCode: "AC2", 
    Date: "2016-01-14T00:00:00", 
    ID: 12347, 
    Measure1: 12345, 
    Measure2: -1234 
}] 
} 
+0

這很棒,我可以循環並根據需要分配值。 – LCaraway

0

我真的不明白你的代碼是試圖完成,但如果你想要做的是遍歷對象在你的JSON數組中,那麼這更簡單:

<script> 
    var 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" } ]'; 
    var response = JSON.parse(json); 

    for(var i = 0, j = response.length; i < j; i++) { 
     var object = response[i]; 
     console.log(object); 
     var measure3 = object.Measure1 + object.Measure2; 
     console.log(measure3); 
    } 
</script> 
+0

我的目標是完成您在那裏完成的任務。但是,我想繪製一些結果。我希望在x軸上有日期並在y軸上進行測量,然後顯示按資產代碼分解的數據系列。因此,我試圖將響應JSON解析爲新的對象或最好是數組,以便可以在圖初始化時訪問數據。由於數據每天都會更改,因此腳本需要能夠通過JSON動態分類。 – LCaraway