Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:向不同大小的数组添加0值_Javascript_Arrays_Object_Highcharts_Underscore.js - Fatal编程技术网

Javascript:向不同大小的数组添加0值

Javascript:向不同大小的数组添加0值,javascript,arrays,object,highcharts,underscore.js,Javascript,Arrays,Object,Highcharts,Underscore.js,解释起来相当复杂,所以我会尽量明确 我有一个对象data,它是一个对象数组,包括一个对象数组和一个字符串: var groups = [ { bars: [ {label: 'a', value: 28}, {label: 'c', value: 16} ], string: 'one' }, { bars: [ {label: 'a'

解释起来相当复杂,所以我会尽量明确

我有一个对象data,它是一个对象数组,包括一个对象数组和一个字符串:

var groups = [
    { 
        bars: [
            {label: 'a', value: 28},
            {label: 'c', value: 16}
        ],
        string: 'one'
    },
    {
        bars: [
            {label: 'a', value: 3},
            {label: 'b', value: 17},
            {label: 'c', value: 24}
        ],
        string: 'two'
    },
    {
        bars: [
            {label: 'a', value: 12},
            {label: 'd', value: 7},
            {label: 'e', value: 36}
        ],
        string: 'three'
    },
    {
        bars: [
            {label: 'c', value: 32},
            {label: 'e', value: 2}
        ],
        string: 'four'
    }
] 
我需要所有“条”对象的大小相同,如果所述标签的对象不存在,则其值为0。如您所见,与“条”关联的标签键是一个有限列表。我这里的主要问题是“groups”对象和“bars”对象的大小都是动态的。在下划线中,我使用了:

var labelNames = _.uniq(_.flatten
    (_.map(data.groups,function(groups) {
    return _.pluck(groups.bars, 'label');
})));
提取已知标签的列表给我

['a', 'b', 'c', 'd', 'e']
现在,我想将此数组映射到bars对象,以提供以下输出:

var groups = [
    { 
        bars: [
            {label: 'a', value: 28},
            {label: 'b', value: 0},
            {label: 'c', value: 16},
            {label: 'd', value: 0},
            {label: 'e', value: 0}
        ],
        string: 'one'
    },
    {
        bars: [
            {label: 'a', value: 3},
            {label: 'b', value: 17},
            {label: 'c', value: 24},
            {label: 'd', value: 0},
            {label: 'e', value: 0}
        ],
        string: 'two'
    },
    {
        bars: [
            {label: 'a', value: 12},
            {label: 'b', value: 0},
            {label: 'c', value: 0},
            {label: 'd', value: 7},
            {label: 'e', value: 36}
        ],
        string: 'three'
    },
    {
        bars: [
            {label: 'a', value: 0},
            {label: 'b', value: 0},
            {label: 'c', value: 32},
            {label: 'd', value: 0},
            {label: 'e', value: 2}
        ],
        string: 'four'
    }
]
如果有帮助的话,我甚至必须有条形图,因为我已经用这些对象创建了一个highcharts系列。
我希望这是有道理的。任何帮助都将不胜感激。我一直在发疯想弄明白这一点

这应该可以做到:

// First, iterate over each group
for (var i = 0; i < groups.length; i++) {

    var bars = groups[i].bars;

    // Second, iterate over each of the previously collected labels, e.g. var labels = ['a', 'b', 'c', 'd', 'e']
    for (var j = 0; j < labels.length; j++) {

        // Assume label does not exist, until proven otherwise
        var labelDoesNotExist = true;

        // Third, iterate over the bars' existing labels
        for (var k = 0; k < bars.length; k++) {

            var label = bars[k].label;

            // Check if the label matches the current iteration of pre-collected labels
            if (label == labels[j]) {
                labelDoesNotExist = false;
            }
        }

        // If no existing label matches any of the pre-collected labels, then create a new label with value of 0
        if (labelDoesNotExist) {
            bars.push({
                'label': labels[j],
                'value': '0'
            });
        }
    }
}
//首先,迭代每个组
对于(变量i=0;i
以下是我为那些偶然发现这一点的人准备的

_.each(data.groups, function(group) {
    var currentValues = _.pluck(group.bars, 'label');
    var zeroValues = _.difference(labelNames, currentValues);
    _.each(zeroValues, function(newLabel) {
        group.bars.push({label: newLabel, value: 0});
    });
}); 
功能性和ECMAScript 6替代方案:

首先,获取标签:['a','b',c',…]

let labels = groups
  .reduce((res, item) => 
    [...res, ...item.bars.map(b => b.label)], 
  [])
  .filter((value, index, self) => 
    self.indexOf(value) === index
  )
  .sort()
使用reduce和concat节省时间

第二步,在没有供应商库的情况下生成作业:

let newgroups = groups.map(item  => 
  Object.assign({}, item, {bars : [
    ...item.bars, 
    ...labels.filter(l => 
        !item.bars.some(b => b.label == l)
      )
      .map(l => ({label:l, value:0}))
  ].sort((a, b) => a.label < b.label ? -1:1)})
);
让newgroups=groups.map(项=>
赋值({},项,{bar:[
…项.bar,
…labels.filter(l=>
!item.bar.some(b=>b.label==l)
)
.map(l=>({label:l,值:0}))
].sort((a,b)=>a.label
最后的
sort
是可选的,但是bars-Iten可以更好地使用它

在镀铬和实际发动机上工作,不带Transbile。
ES6是生命

循环通过
组的每个元素
,每个
。使用
.pulk
获取
条的所有标签,使用
.difference
获取缺少的标签。然后添加带有这些标签的元素和
值:0
。谢谢!这让我了解了大部分情况,因此我将其作为答案发布。非常好,真正展示了下划线.js的强大功能。这是一个更具可读性的替代方案