将新元素添加到对象中后,我一直得到空值

时间:2019-05-29 08:42:44

标签: javascript json

我有一个具有相似值的数组对象。我使用循环排除了它们的重复值,然后将值添加到其他对象(objectProperties)中,效果很好,但是在类别中却得到了NULL值

   // data which im extracting
var data = [ 
   {
      "label":"May 14",
      "value":56714.4,
      "proID":"ISB"
   },
   {
      "label":"May 14",
      "value":15902.5,
      "proID":"LI8"
   },
   {
      "label":"May 14",
      "value":419.6,
      "proID":"TR2"
   },
   {
      "label":"May 15",
      "value":2754.8,
      "proID":"DAC"
   },
   {
      "label":"May 15",
      "value":50845.7,
      "proID":"ISB"
   },
   {
      "label":"May 15",
      "value":19760.3,
      "proID":"LI8"
   },
   {
      "label":"May 15",
      "value":1704.1,
      "proID":"TR2"
   },
   {
      "label":"May 16",
      "value":2145.6,
      "proID":"DAC"
   },
   {
      "label":"May 16",
      "value":55666.4,
      "proID":"ISB"
   },
   {
      "label":"May 16",
      "value":15044.4,
      "proID":"LI8"
   },
   {
      "label":"May 16",
      "value":2413.5,
      "proID":"TR2"
   },
   {
      "label":"May 17",
      "value":6564.4,
      "proID":"DAC"
   },
   {
      "label":"May 17",
      "value":71379,
      "proID":"ISB"
   },
   {
      "label":"May 17",
      "value":21774.2,
      "proID":"LI8"
   },
   {
      "label":"May 17",
      "value":2191.4,
      "proID":"TR2"
   },
   {
      "label":"May 18",
      "value":63338.9,
      "proID":"ISB"
   },
   {
      "label":"May 18",
      "value":24451,
      "proID":"LI8"
   },
   {
      "label":"May 18",
      "value":2616.5,
      "proID":"TR2"
   }
];

var propertiesObject = {    // my object
    type: 'mscolumn2d',
     renderAt: 'chart-container',
     width: '1000',
     height: '500',
     dataFormat: 'json', 
     dataSource: {
        chart: {
            caption: "Kilos per Date Comparison"
          
            }, 
    categories: [
            {
                category: [] 
            }
        ]
 
 
    }
 };
 
    var propCount = Object.keys(data).length; // getting object length
    var checkSameLabel = data[0].label;    // for reference reference inside the loop
    var firstIndex = {"label":data[0].label}; // im taking first index of object and add manually
    propertiesObject.dataSource.categories[0].category[0] = firstIndex; // adding the first index to my object
    var currentProject = data[0].proID, counterCurrentProject = 0;
    for(let i = 0; i < propCount; i++) {
        if(checkSameLabel !== data[i].label) { // check if current value of label is not equal then add new data to my object 
            
            const value = data[i].label;
            var obj = { 
                "label": value  
            };
            propertiesObject.dataSource.categories[0].category[i] = value; // adding new data
        }  
        checkSameLabel = data[i].label; // for the next check
    }
    console.log(JSON.stringify(propertiesObject));
    document.getElementById("result").innerHTML = JSON.stringify(propertiesObject);
<div id="result"></div>

我希望输出在“类别”中是这样的

{ "label": "May 14" },
{ "label": "May 15" },
{ "label": "May 16" },
{ "label": "May 17" }

我不知道是因为循环还是我做错了事。

2 个答案:

答案 0 :(得分:1)

使用push代替分配给数组的索引,否则将有空值:

// data which im extracting
var data = [{
    "label": "May 14",
    "value": 56714.4,
    "proID": "ISB"
  },
  {
    "label": "May 14",
    "value": 15902.5,
    "proID": "LI8"
  },
  {
    "label": "May 14",
    "value": 419.6,
    "proID": "TR2"
  },
  {
    "label": "May 15",
    "value": 2754.8,
    "proID": "DAC"
  },
  {
    "label": "May 15",
    "value": 50845.7,
    "proID": "ISB"
  },
  {
    "label": "May 15",
    "value": 19760.3,
    "proID": "LI8"
  },
  {
    "label": "May 15",
    "value": 1704.1,
    "proID": "TR2"
  },
  {
    "label": "May 16",
    "value": 2145.6,
    "proID": "DAC"
  },
  {
    "label": "May 16",
    "value": 55666.4,
    "proID": "ISB"
  },
  {
    "label": "May 16",
    "value": 15044.4,
    "proID": "LI8"
  },
  {
    "label": "May 16",
    "value": 2413.5,
    "proID": "TR2"
  },
  {
    "label": "May 17",
    "value": 6564.4,
    "proID": "DAC"
  },
  {
    "label": "May 17",
    "value": 71379,
    "proID": "ISB"
  },
  {
    "label": "May 17",
    "value": 21774.2,
    "proID": "LI8"
  },
  {
    "label": "May 17",
    "value": 2191.4,
    "proID": "TR2"
  },
  {
    "label": "May 18",
    "value": 63338.9,
    "proID": "ISB"
  },
  {
    "label": "May 18",
    "value": 24451,
    "proID": "LI8"
  },
  {
    "label": "May 18",
    "value": 2616.5,
    "proID": "TR2"
  }
];

var propertiesObject = { // my object
  type: 'mscolumn2d',
  renderAt: 'chart-container',
  width: '1000',
  height: '500',
  dataFormat: 'json',
  dataSource: {
    chart: {
      caption: "Kilos per Date Comparison"

    },
    categories: [{
      category: []
    }]


  }
};

var propCount = Object.keys(data).length; // getting object length
var checkSameLabel = data[0].label; // for reference reference inside the loop
var firstIndex = {
  "label": data[0].label
}; // im taking first index of object and add manually
propertiesObject.dataSource.categories[0].category[0] = firstIndex; // adding the first index to my object
var currentProject = data[0].proID,
  counterCurrentProject = 0;
for (let i = 0; i < propCount; i++) {
  if (checkSameLabel !== data[i].label) { // check if current value of label is not equal then add new data to my object 

    const value = data[i].label;
    var obj = {
      "label": value
    };
    propertiesObject.dataSource.categories[0].category.push(obj);
  }
  checkSameLabel = data[i].label; // for the next check
}
console.log(propertiesObject);
<div id="result"></div>

您还可以通过使用Set跟踪到目前为止添加的标签并forEach遍历数据来简化操作:

// data which im extracting
var data = [{
    "label": "May 14",
    "value": 56714.4,
    "proID": "ISB"
  },
  {
    "label": "May 14",
    "value": 15902.5,
    "proID": "LI8"
  },
  {
    "label": "May 14",
    "value": 419.6,
    "proID": "TR2"
  },
  {
    "label": "May 15",
    "value": 2754.8,
    "proID": "DAC"
  },
  {
    "label": "May 15",
    "value": 50845.7,
    "proID": "ISB"
  },
  {
    "label": "May 15",
    "value": 19760.3,
    "proID": "LI8"
  },
  {
    "label": "May 15",
    "value": 1704.1,
    "proID": "TR2"
  },
  {
    "label": "May 16",
    "value": 2145.6,
    "proID": "DAC"
  },
  {
    "label": "May 16",
    "value": 55666.4,
    "proID": "ISB"
  },
  {
    "label": "May 16",
    "value": 15044.4,
    "proID": "LI8"
  },
  {
    "label": "May 16",
    "value": 2413.5,
    "proID": "TR2"
  },
  {
    "label": "May 17",
    "value": 6564.4,
    "proID": "DAC"
  },
  {
    "label": "May 17",
    "value": 71379,
    "proID": "ISB"
  },
  {
    "label": "May 17",
    "value": 21774.2,
    "proID": "LI8"
  },
  {
    "label": "May 17",
    "value": 2191.4,
    "proID": "TR2"
  },
  {
    "label": "May 18",
    "value": 63338.9,
    "proID": "ISB"
  },
  {
    "label": "May 18",
    "value": 24451,
    "proID": "LI8"
  },
  {
    "label": "May 18",
    "value": 2616.5,
    "proID": "TR2"
  }
];

var propertiesObject = { // my object
  type: 'mscolumn2d',
  renderAt: 'chart-container',
  width: '1000',
  height: '500',
  dataFormat: 'json',
  dataSource: {
    chart: {
      caption: "Kilos per Date Comparison"

    },
    categories: [{
      category: []
    }]
  }
};

const labelsAdded = new Set();
data.forEach(({ label }) => {
  if (labelsAdded.has(label)) {
    return;
  }
  labelsAdded.add(label);
  propertiesObject.dataSource.categories[0].category.push({ label });
});
console.log(propertiesObject);

或者,通过创建一组标签字符串,然后使用.map

// data which im extracting
var data = [{
    "label": "May 14",
    "value": 56714.4,
    "proID": "ISB"
  },
  {
    "label": "May 14",
    "value": 15902.5,
    "proID": "LI8"
  },
  {
    "label": "May 14",
    "value": 419.6,
    "proID": "TR2"
  },
  {
    "label": "May 15",
    "value": 2754.8,
    "proID": "DAC"
  },
  {
    "label": "May 15",
    "value": 50845.7,
    "proID": "ISB"
  },
  {
    "label": "May 15",
    "value": 19760.3,
    "proID": "LI8"
  },
  {
    "label": "May 15",
    "value": 1704.1,
    "proID": "TR2"
  },
  {
    "label": "May 16",
    "value": 2145.6,
    "proID": "DAC"
  },
  {
    "label": "May 16",
    "value": 55666.4,
    "proID": "ISB"
  },
  {
    "label": "May 16",
    "value": 15044.4,
    "proID": "LI8"
  },
  {
    "label": "May 16",
    "value": 2413.5,
    "proID": "TR2"
  },
  {
    "label": "May 17",
    "value": 6564.4,
    "proID": "DAC"
  },
  {
    "label": "May 17",
    "value": 71379,
    "proID": "ISB"
  },
  {
    "label": "May 17",
    "value": 21774.2,
    "proID": "LI8"
  },
  {
    "label": "May 17",
    "value": 2191.4,
    "proID": "TR2"
  },
  {
    "label": "May 18",
    "value": 63338.9,
    "proID": "ISB"
  },
  {
    "label": "May 18",
    "value": 24451,
    "proID": "LI8"
  },
  {
    "label": "May 18",
    "value": 2616.5,
    "proID": "TR2"
  }
];

var propertiesObject = { // my object
  type: 'mscolumn2d',
  renderAt: 'chart-container',
  width: '1000',
  height: '500',
  dataFormat: 'json',
  dataSource: {
    chart: {
      caption: "Kilos per Date Comparison"

    },
    categories: [{
      category: [...new Set(data.map(({ label }) => label))].map(label => ({ label }))
    }]
  }
};
console.log(propertiesObject);

答案 1 :(得分:0)

有一些错误

  1. 您创建了变量'obj',但未使用它

var obj = { 
    "label": value  
};

// No good
propertiesObject.dataSource.categories[0].category[i] = value;

// Should be this, but still not correct, see point (2)
propertiesObject.dataSource.categories[0].category[i] = obj;
  1. 您要通过使用索引设置将元素添加到数组中,而应使用数组推送
// No good
propertiesObject.dataSource.categories[0].category[i] = obj;

// Should be
propertiesObject.dataSource.categories[0].category.push(obj);

// data which im extracting
var data = [ 
   {
      "label":"May 14",
      "value":56714.4,
      "proID":"ISB"
   },
   {
      "label":"May 14",
      "value":15902.5,
      "proID":"LI8"
   },
   {
      "label":"May 14",
      "value":419.6,
      "proID":"TR2"
   },
   {
      "label":"May 15",
      "value":2754.8,
      "proID":"DAC"
   },
   {
      "label":"May 15",
      "value":50845.7,
      "proID":"ISB"
   },
   {
      "label":"May 15",
      "value":19760.3,
      "proID":"LI8"
   },
   {
      "label":"May 15",
      "value":1704.1,
      "proID":"TR2"
   },
   {
      "label":"May 16",
      "value":2145.6,
      "proID":"DAC"
   },
   {
      "label":"May 16",
      "value":55666.4,
      "proID":"ISB"
   },
   {
      "label":"May 16",
      "value":15044.4,
      "proID":"LI8"
   },
   {
      "label":"May 16",
      "value":2413.5,
      "proID":"TR2"
   },
   {
      "label":"May 17",
      "value":6564.4,
      "proID":"DAC"
   },
   {
      "label":"May 17",
      "value":71379,
      "proID":"ISB"
   },
   {
      "label":"May 17",
      "value":21774.2,
      "proID":"LI8"
   },
   {
      "label":"May 17",
      "value":2191.4,
      "proID":"TR2"
   },
   {
      "label":"May 18",
      "value":63338.9,
      "proID":"ISB"
   },
   {
      "label":"May 18",
      "value":24451,
      "proID":"LI8"
   },
   {
      "label":"May 18",
      "value":2616.5,
      "proID":"TR2"
   }
];

var propertiesObject = {    // my object
    type: 'mscolumn2d',
     renderAt: 'chart-container',
     width: '1000',
     height: '500',
     dataFormat: 'json', 
     dataSource: {
        chart: {
            caption: "Kilos per Date Comparison"
          
            }, 
    categories: [
            {
                category: [] 
            }
        ]
 
 
    }
 };
 
    var propCount = Object.keys(data).length; // getting object length
    
    console.log(propCount)
    
    var checkSameLabel = data[0].label;    // for reference reference inside the loop
    var firstIndex = {"label":data[0].label}; // im taking first index of object and add manually
    propertiesObject.dataSource.categories[0].category[0] = firstIndex; // adding the first index to my object
    var currentProject = data[0].proID, counterCurrentProject = 0;
    for(let i = 0; i < propCount; i++) {
        if(checkSameLabel != data[i].label) { // check if current value of label is not equal then add new data to my object 
            
            const value = data[i].label;
            var obj = { 
                "label": value  
            };

            // Use Array.push() to add new data
            propertiesObject.dataSource.categories[0].category.push(obj); 
        }  
        checkSameLabel = data[i].label; // for the next check
    }
    console.log(JSON.stringify(propertiesObject));
    document.getElementById("result").innerHTML = JSON.stringify(propertiesObject);
<div id="result"></div>