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

Javascript: Adding 0 values to arrays of different sizes

解释起来很复杂,所以我会尽量明确。

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

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'
    }
] 

我需要所有 "bars" 个对象具有相同的大小,并且如果所述标签的对象不存在则具有 0 值。如您所见,与 "bars" 关联的标签键是一个有限列表。我这里的主要问题是 "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 系列 like this example。 我希望这是有道理的。任何帮助,将不胜感激。我一直在疯狂地想弄清楚这一点。

这应该可以解决问题:

// 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'
            });
        }
    }
}

以下是我为那些偶然发现的人提供的帮助。

_.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});
    });
}); 

功能性和 EcmaScript6 替代方案:

首先,获取标签:['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()

Saving the day with reduce and 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)})
);

The final sort is opcional, but bars itens can be better with this.

适用于 Chrome 和没有转译的实际引擎。 ES6就是生命