对象数组,在条件满足时更改为新结构

时间:2017-01-15 20:36:04

标签: javascript arrays

给出以下数组:我希望按timestamp对所有内容进行分组,并real subGroup符合mainGroup符合var arr = [ { mainGroup: 'A', subGroup: 'B', real: 100, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'A', subGroup: 'C', real: 150, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'D', real: 123, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'Y', real: 542, timestamp: '2017-01-15T01:00:00.000Z' } ] 。见下面的结果我......之后......

[
  {
    A: 250, // 250 is sub B real + sub C real.
    B: 665, // 665 is sub D real + sub Y real.
    timestamp: '2017-01-15T01:00:00.000Z'
  },
]

结果将是:

var arr = [
  {
    mainGroup: 'A',
    subGroup: 'B',
    real: 100,
    timestamp: '2017-01-15T01:00:00.000Z'
  },
  {
    mainGroup: 'A',
    subGroup: 'C',
    real: 150,
    timestamp: '2017-01-15T01:00:00.000Z'
  },
  {
    mainGroup: 'B',
    subGroup: 'D',
    real: 123,
    timestamp: '2017-01-15T01:00:00.000Z'
  },
  {
    mainGroup: 'B',
    subGroup: 'Y',
    real: 542,
    timestamp: '2017-01-15T01:00:00.000Z'
  }
]

let b = arr.reduce((a, item) => {
  a[item.timestamp] = a[item.timestamp] || {};

  Object.assign(a[item.timestamp], {
    [item.mainGroup]: item.real
  });

  return a;
}, {});

Object.keys(b)
    .map(timestamp => Object.assign(b[timestamp], { timestamp }));

console.log(b)

到目前为止我的代码:



  function onlyNthCall(callCount, callback)
    {
    var callIter = 0;
    return function()
      {
      callIter++;
      if(callIter === callCount) { callback(); }
      }
    }
var task2AfterLastCall = onlyNthCall(Object.keys(this.words).length, task2);

for(let word in this.words){
    var i = Object.keys(this.words).indexOf(word);

    this.convertor.get(this.words[i].value).subscribe( function (i, result) {
    this.words[i].value = result; 
    task2AfterLastCall();
  }.bind(this, i));
}




2 个答案:

答案 0 :(得分:1)

a)正确初始化您的累加器初始值

b)总和A,B值 - 您重新分配值(覆盖)

Random masterRand = new Random();
for (int s = 0; s < 600; s++) {
  Random r = new Random(masterRand.nextLong());
  System.out.println(r.nextDouble());
  System.out.println(r.nextDouble() + "\n-----");
}

或者,代码缩减形式:

arr.reduce(function(acc, next){
    if (next.timestamp) {
        acc.timestamp = next.timestamp;
    }

    if (acc[next.mainGroup] === undefined) {
       acc[next.mainGroup] = 0;
    }

    acc[next.mainGroup] += next.real;

    return acc;
}, {})

答案 1 :(得分:1)

您可以使用对象来引用该组。

Array#forEach

&#13;
&#13;
var array = [{ mainGroup: 'A', subGroup: 'B', real: 100, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'A', subGroup: 'C', real: 150, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'D', real: 123, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'Y', real: 542, timestamp: '2017-01-15T01:00:00.000Z' }],
    grouped = [];

array.forEach(function (a) {
    if (!this[a.timestamp]) {
        this[a.timestamp] = { timestamp: a.timestamp };
        grouped.push(this[a.timestamp]);
    }
    this[a.timestamp][a.mainGroup] = (this[a.timestamp][a.mainGroup] || 0) + a.real;
}, Object.create(null));

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

Array#reduce

&#13;
&#13;
var array = [{ mainGroup: 'A', subGroup: 'B', real: 100, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'A', subGroup: 'C', real: 150, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'D', real: 123, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'Y', real: 542, timestamp: '2017-01-15T01:00:00.000Z' }],
    grouped = array.reduce(function (hash) {
        return function (r, a) {
            if (!hash[a.timestamp]) {
                hash[a.timestamp] = { timestamp: a.timestamp };
                r.push(hash[a.timestamp]);
            }
            hash[a.timestamp][a.mainGroup] = (hash[a.timestamp][a.mainGroup] || 0) + a.real;
            return r;
        };
    }(Object.create(null)), []);

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

ES6与Array#reduce

&#13;
&#13;
var array = [{ mainGroup: 'A', subGroup: 'B', real: 100, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'A', subGroup: 'C', real: 150, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'D', real: 123, timestamp: '2017-01-15T01:00:00.000Z' }, { mainGroup: 'B', subGroup: 'Y', real: 542, timestamp: '2017-01-15T01:00:00.000Z' }],
    grouped = array.reduce((hash => (r, a) => {
        if (!hash[a.timestamp]) {
            hash[a.timestamp] = { timestamp: a.timestamp };
            r.push(hash[a.timestamp]);
        }
        hash[a.timestamp][a.mainGroup] = (hash[a.timestamp][a.mainGroup] || 0) + a.real;
        return r;
    })(Object.create(null)), []);

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;