展平Javascript对象数组

时间:2016-10-03 23:46:18

标签: javascript arrays each

我有一个对象:

[
  {
    "DATA": "2016-01-22",
    "TOTAL": "7"
  },
  {
    "DATA": "2016-01-25",
    "TOTAL": "3"
  },
  {
    "DATA": "2016-01-26",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-27",
    "TOTAL": "2"
  },
  {
    "DATA": "2016-01-22",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-25",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-27",
    "TOTAL": "1"
  }, 
 ...
]

如何将其缩小到类似下面的内容,这是连接/加入日期相同的TOTAL键,如果日期不重复,则填0;

[
  {
    "DATA": "2016-01-22",
    "TOTAL": ["7", "1"]
  },
  {
    "DATA": "2016-01-25",
    "TOTAL": ["3", "1"]
  },
  {
    "DATA": "2016-01-26",
    "TOTAL": ["1", "0"]
  },
  {
    "DATA": "2016-01-27",
    "TOTAL": ["2", "1"]
  }
]

我一直在尝试使用这段代码,但无法获得所有相同维度的TOTAL键 - 用零填充就可以了。

var output = [];
d.forEach(function(value) {
    var existing = output.filter(function(v, i) {
        return v.DATA == value.DATA;
    });
    if (existing.length) {
        var existingIndex = output.indexOf(existing[0]);
        output[existingIndex].TOTAL = output[existingIndex].TOTAL.concat(value.TOTAL);
    } else {
        if (typeof value.TOTAL == 'string')
            value.TOTAL = [value.TOTAL];
        output.push(value);
    }
});
console.log(JSON.stringify(output, null, 4));

2 个答案:

答案 0 :(得分:1)

var someData = [] // <- your instantiated array in question.
var transformedData = [];

var highestCount = 0;

someData.forEach(x => {
    var foundIndex = transformedData.findIndex((ele) => ele.DATA === x.DATA);
    if (foundIndex < 0) {
        transformedData
            .push({DATA : x.DATA, TOTAL : [x.TOTAL]});
    } else {
        transformedData[foundIndex]
            .TOTAL.push(x.TOTAL);
        var currentCountAtIndex = transformedData[foundIndex].TOTAL.length;
        if (highestCount < transformedData[foundIndex].TOTAL.length) highestCount = currentCountAtIndex;
    }
});

// fill any indicies in array that are lower than the highest count with 0
transformedData
    .forEach(x => {
        if (x.TOTAL.length < highestCount) {
            while(x.TOTAL.length < highestCount) {
                x.TOTAL.push(0);
            }
        }
    });

答案 1 :(得分:0)

可能就是这么简单:

&#13;
&#13;
var result = {};
var test = [
  {
    "DATA": "2016-01-22",
    "TOTAL": "7"
  },
  {
    "DATA": "2016-01-25",
    "TOTAL": "3"
  },
  {
    "DATA": "2016-01-26",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-27",
    "TOTAL": "2"
  },
  {
    "DATA": "2016-01-22",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-25",
    "TOTAL": "1"
  },
  {
    "DATA": "2016-01-27",
    "TOTAL": "1"
  }];

console.log("test array: ", test);

var len = 0,
sorted;

// Flatten the object.
test.forEach( d => {
   result[d.DATA] == undefined ? result[d.DATA] = [d.TOTAL] : result[d.DATA].push(d.TOTAL);
});

// Sort so we get the max length to know how many zeros to add.
sorted = Object.keys(result).sort( (k, b) => {
   return result[k].length - result[b].length;
});

// Max length from the sorted array. 
len = result[sorted[sorted.length - 1]].length;

// push zeros
Object.keys(result).forEach( k => {
   if(result[k].length < len){
  for(var i = result[k].length; i < len; i++){
     result[k].push("0");
  }
   }
});

console.log("result: ", result);
&#13;
&#13;
&#13;