处理从javascript对象获取的数据

时间:2019-06-06 02:11:29

标签: javascript json

我的代码需要帮助 我有这个数据对象,这是我的数据源。

    var data = [   {
      "label":"May 7",
      "value":25736.6,
      "proID":"ISB"
   },
   {
      "label":"May 8",
      "value":7893.3,
      "proID":"LI8"
   },
   {
      "label":"May 8",
      "value":30972.3,
      "proID":"ISB"
   },
   {
      "label":"May 9",
      "value":25060.7,
      "proID":"ISB"
   },
   {
      "label":"May 9",
      "value":17920.5,
      "proID":"LI8"
   },
   {
      "label":"May 10",
      "value":16209.8,
      "proID":"LI8"
   },
   {
      "label":"May 10",
      "value":62440.4,
      "proID":"ISB"
   },
   {
      "label":"May 11",
      "value":14433.6,
      "proID":"LI8"
   },
   {
      "label":"May 11",
      "value":48848.5,
      "proID":"ISB"
   }
   ];

我正在处理这些数据以提供我的另一种对象结构:

  const labelsAdded = new Set();
  data.forEach(({ label }) => {
    if (labelsAdded.has(label)) {
      return;
    } 
    labelsAdded.add(label);
    propertiesObject.dataSource.categories[0].category.push({ label });
  });
 // alert(labelsAdded.size);
  var propCount = Object.keys(data).length;
  let setLabel = [...labelsAdded];
  for(let i = 0; i < labelsAdded.size; i++) {
     for(let x = 0; x < propCount; x++) {

   if(data[x].proID == 'ISB') {
          propertiesObject.dataSource.dataset[0].data.push({value: data[x].value});
        } 

        if(data[x].proID == 'LI8') {
          propertiesObject.dataSource.dataset[1].data.push({value: data[x].value});
        } 

        if(data[x].proID == 'DAC') {
          propertiesObject.dataSource.dataset[1].data.push({value: data[x].value});
        } 
     } 
}

我的问题是 我如何动态地将提取的数据放到正确的日期 数据中的示例1

  {
  "label":"May 8",
  "value":7893.3,
  "proID":"LI8"
  }

因为这是5月8日,所以它应该放置在5月8日,但是如果您检查我的jsfiddle,它放置在5月7日,这会导致我的图形不正确,因为它只是填充我的对象而没有检查它是否正确位置。

请在这里http://jsfiddle.net/meraki26/342b6a0w/10/检查我的jsfiddle,如果我的问题不清楚,您可以看到我提到的问题 enter image description here

2 个答案:

答案 0 :(得分:2)

 var propCount = Object.keys(data).length;
  let setLabel = [...labelsAdded];
  for(let i = 0; i < labelsAdded.size; i++) {
     for(let x = 0; x < propCount; x++) {

        if(data[x].proID == 'ISB') {
            let ndx = Array.from(setLabel).indexOf(data[x].label);
          propertiesObject.dataSource.dataset[0].data.push({value: null});
          propertiesObject.dataSource.dataset[0].data[ndx]=data[x];
        } 

        if(data[x].proID == 'LI8') {
          let ndx = Array.from(setLabel).indexOf(data[x].label);
          propertiesObject.dataSource.dataset[1].data.push({value: null});
          propertiesObject.dataSource.dataset[1].data[ndx]=data[x];
        } 

        if(data[x].proID == 'DAC') {
          let ndx = Array.from(setLabel).indexOf(data[x].label);
          propertiesObject.dataSource.dataset[2].data.push({value: null});
          propertiesObject.dataSource.dataset[2].data[ndx]=data[x];
        } 
     }
  }

在这里,我仅在与标签完美匹配的同一索引处插入了数据 我还在空白处添加了空值 好问题!

LINK: http://jsfiddle.net/3ukj6L9q/45/

答案 1 :(得分:1)

我认为您的代码将所有值都放在了日期中。
可能这是正确的:

var data = [{ label: "May 7",  value: 25736.6, proID: "ISB" }
          , { label: "May 8",  value: 7893.3,  proID: "LI8" }
          , { label: "May 8",  value: 30972.3, proID: "ISB" }
          , { label: "May 9",  value: 25060.7, proID: "ISB" }
          , { label: "May 9",  value: 17920.5, proID: "LI8" }
          , { label: "May 10", value: 16209.8, proID: "LI8" }
          , { label: "May 10", value: 62440.4, proID: "ISB" }
          , { label: "May 11", value: 14433.6, proID: "LI8" }
          , { label: "May 11", value: 48848.5, proID: "ISB" }
          ];

const Prop = { 'ISB': 0,  'LI8': 1,  'DAC': 1 }

const labelsAdded = new Set();

for (let {label} of data)
{
  if (labelsAdded.has(label)) continue 
  labelsAdded.add(label);
  propertiesObject.dataSource.categories[0].category.push({ label });
}

for (let xDate of labelsAdded) 
{
  for( let OnDate of data.filter(d=>d.label===xDate))
  {
    propertiesObject.dataSource.dataset[ Prop[OnDate.proID] ].data.push({value: OnDate.value});
  }
}