JavaScript - 2D数组/对象

时间:2016-01-15 18:47:42

标签: javascript arrays json

我正在尝试创建数组和/或对象来保存图形的值。数据以JSON格式显示。每个对象都有一个资产代码,一个日期和我感兴趣的两个测量值。(测量3是两者的净值)我试图通过资产代码和数据捕获日期进行过滤。我想将它编译成一个单独的对象,我可以循环并按日期提取每个资产的数据。我的尝试下面是创建一个数组数组但是遇到了一些未定义的变量问题。有没有更简洁的方法来实现这一目标?

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"
   }
]

2 个答案:

答案 0 :(得分: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
}]
}

答案 1 :(得分: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>