将嵌套对象推入数组

时间:2019-07-02 16:36:38

标签: javascript arrays

如何将嵌套对象推入具有如下结构的数组?

var objectData = 
{
    dataset1: 
    [
        {
            "label": "Last Week",
            "values":
            [
                { value: "20000" },
                { value: "14000" },
                { value: "12000" },
                { value: "15000" },
                { value: "18000" },
                { value: "19000" },
                { value: "22000" }                      
            ]                       
        },
        {
            "label": "This Week",
            "values":
            [
                { value: "30000" },
                { value: "18000" },
                { value: "17000" },
                { value: "15000" },
                { value: "13000" },
                { value: "12000" },
                { value: "25000" }                      
            ]                       
        }                   
    ]
};

var label;
var data = [];

var chartData = {
  labels: [],
  datasets: []
};

var ds = [{
  label: label,
  data: []
}];

jQuery.each(this.getAggregation("axes"), function(itmidx, item) {
  chartData.labels.push(item.getText());
});

var v = objectData;

for (var i = 0; i < v.length; i++) {
  label = v[i].label;

  for (var j = 0; j < v[i].values.length; j++) {
    ds.data.push(v[i].values[j].value);
  }
  ds.label = label;

  chartData.datasets.push(ds);
}

数据应如下所示,但将标签“ This Week”的值附加到标签“ Last Week”的值上,并对两个索引重复该数据。我试图用ds = []清除数组,但也会删除推送的数据。

            dataset1: 
            [
                {
                    "label": "Last Week",
                    "values":
                    [
                        { value: "20000" },
                        { value: "14000" },
                        { value: "12000" },
                        { value: "15000" },
                        { value: "18000" },
                        { value: "19000" },
                        { value: "22000" }                      
                    ]                       
                },
                {
                    "label": "This Week",
                    "values":
                    [
                        { value: "30000" },
                        { value: "18000" },
                        { value: "17000" },
                        { value: "15000" },
                        { value: "13000" },
                        { value: "12000" },
                        { value: "25000" }                      
                    ]                       
                }                   
            ]

1 个答案:

答案 0 :(得分:0)

您好,您的问题还不够清楚,但是我想您想将所有标签放入一个新数组,并将所有值放入另一个数组,这是解决方案。让我知道我是否做错了假设

var label;
var data = [];

var chartData = {
  labels: [],
  datasets: []
};


var objectData = 
{
    dataset1: 
    [
        {
            "label": "Last Week",
            "values":
            [
                { value: "20000" },
                { value: "14000" },
                { value: "12000" },
                { value: "15000" },
                { value: "18000" },
                { value: "19000" },
                { value: "22000" }                      
            ]                       
        },
        {
            "label": "This Week",
            "values":
            [
                { value: "30000" },
                { value: "18000" },
                { value: "17000" },
                { value: "15000" },
                { value: "13000" },
                { value: "12000" },
                { value: "25000" }                      
            ]                       
        }                   
    ]
};

const {dataset1} = objectData;

dataset1.map(({label, values}) => 
{
  const datasetsArr = values.map(({value}) => value );
  chartData.labels.push(label);
  chartData.datasets.push(datasetsArr)

});


console.log(chartData);