创建不同的数据集数组

时间:2017-08-28 08:12:40

标签: javascript json

我正在尝试根据月份值创建不同的数据集。例如。 6月份的一个数据集和7月的另一个数据集。但是在我的代码中,所有月份值都被合并并创建为一个数据集。

能帮助我动态创建不同数据集真的很有帮助。我附上了我用数据对象试过的小提琴

JSFIDDLE

var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }]
  var maindataset = [];
  var dataset = [];
 ["reqC", "resC"].forEach((series) => {
    dataset.push({
      seriesname: series,
      data: obj.map((el) => {
        return el[series]
      })
    })

  });
  maindataset.push({
    dataset: dataset
  });
  alert(JSON.stringify(maindataset));

// Expected Output  

{
  "dataset": [
    {
      "dataset": [                 //June
        {
          "seriesname": "Req",   
          "data": [
            {
              "value": "129963"
            },
            {
              "value": "261162"
            }
          ]
        },
        {
          "seriesname": "Res",
          "data": [
            {
              "value": "80522"
            },
            {
              "value": "83743"
            }
          ]
        }
      ]
    },
    {
      "dataset": [                   //July
        {
          "seriesname": "Req",
          "data": [
            {
              "value": "438860"
            }
          ]
        },
        {
          "seriesname": "Res",
          "data": [
            {
              "value": "166107"
            }
          ]
        }
      ]
    }
  ]
}

4 个答案:

答案 0 :(得分:1)

您可以使用嵌套哈希表并稍后迭代所需部分的键。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container" id="divRed"></div>
<div class="container" id="divBlue"></div>
<div class="container" id="divGreen"></div>
<div class="container" id="divYellow"></div>
var data = [{ date: "2017-06-01", reqC: "129963", month: "JUNE", resC: "80522" }, { date: "2017-06-05", reqC: "261162", month: "JUNE", resC: "83743" }, { date: "2017-07-03", reqC: "438860", month: "JULY", resC: "166107" }],
    result = { dataset: [] },
    parts = { reqC: 'Req', resC: 'Res' },
    hash = { _: result.dataset };

data.forEach(function (a) {
    var temp = hash;
    if (!temp[a.month]) {
        temp[a.month] = { _: [] };
        temp._.push({ dataset: temp[a.month]._ });
    }
    temp = temp[a.month];
    Object.keys(parts).forEach(function (k) {
        if (!temp[k]) {
            temp[k] = { _: [] };
            temp._.push({ seriesname: parts[k], data: temp[k]._ });
        }
        temp[k]._.push({ value: a[k] });
    });
});

console.log(result);

答案 1 :(得分:1)

您可以根据月份创建组,然后您可以输出所需的数据结构。检查代码段。

var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }];

var result = {};

var groups = obj.reduce(function(acc, obj) {
  acc[obj.month] = acc[obj.month] || [];
  acc[obj.month].push(obj);
  return acc;
}, {});

//console.log(groups);

result.dataset = Object.keys(groups).map(function(key) {
  return {
    dataset: [{
      "seriesname" : "Req",   
      "data": groups[key].map(function(o) {
        return { value : o.reqC };
      })
    }, {
      "seriesname" : "Res",   
      "data": groups[key].map(function(o) {
        return { value : o.resC };
      })
    }]
  };
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您不能对对象使用相同的属性名称两次。您的数据中有一个对象如下所示:

"data": [
      {
        "value": "80522"
      },
      {
        "value": "83743"
      }
    ]

将密钥更改为唯一密钥:

"data": [
      {
        "value1": "80522"
      },
      {
        "value2": "83743"
      }
    ]

或者把它变成一个数组:

"data": [ "80522", "83743" ]

答案 3 :(得分:0)

您还需要添加月份检查。 试试这个:

    var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }]
  var maindataset = [];

  ["JUNE","JULY"].forEach((month)=>{
  var dataset = [];
     ["reqC", "resC"].forEach((series) => {
      dataset.push({
        seriesname: series,
        data: obj.reduce((filtered, el) => {
          if(el["month"] === month){
           filtered.push({value: el[series]});
           }
           return filtered;
        },[])
      })

    });
    maindataset.push({
      dataset: dataset
    });
  })

  alert(JSON.stringify(maindataset));

输出:

[{
    "dataset": [{
        "seriesname": "reqC",
        "data": [{
            "value": "129963"
        }, {
            "value": "261162"
        }]
    }, {
        "seriesname": "resC",
        "data": [{
            "value": "80522"
        }, {
            "value": "83743"
        }]
    }]
}, {
    "dataset": [{
        "seriesname": "reqC",
        "data": [{
            "value": "438860"
        }]
    }, {
        "seriesname": "resC",
        "data": [{
            "value": "166107"
        }]
    }]
}]