如何将索引为 0 处重复项的一组数组合并为一个数组 javascript

How to merge an array of arrays with duplicate at index 0 into one array javascript

我有一个数组数组,日期在索引 0 处。我很想检查是否喜欢循环遍历数组以及与另一个匹配的任何日期,应该合并这些值。

这是数组的样子,也是我尝试过的..

var array = [
  [
    Date 2019-06-11T10:00:00.000Z,
    0,
    0,
    0,
    23
  ],
  [
    Date 019-06-11T10:00:00.000Z,
    0,
    0,
    2,
    0
  ],
  [
   Date 2019-16-11T12:00:00.000Z,
    0,
    56,
    0,
    0
  ],
  [
    Date 2019-16-11T12:00:00.000Z,
    3,
    0,
    0,
    0
  ]
]

var result = array.filter(function(v) {
  return this[v[0]] ?
    !Object.assign(this[v[0]], v) :
    (this[v[0]] = v)
}, []);
console.log(result);

我希望输出是这样的,但该方法似乎删除了重复项。

var array = [[
  Date  2019-06-11T10:00:00.000Z,
    0,
    0,
    2,
    23
  ],[
   Date 2019-16-11T12:00:00.000Z,
    3,
    56,
    0,
    0
  ]]

an image of what the data looks like on the browser console

快速接近对象。

var array = [[new Date('2019-06-11T10:00:00.000Z'), 0, 0, 0, 23], [new Date('2019-06-11T10:00:00.000Z'), 0, 0, 2, 0], [new Date('2019-06-16T12:00:00.000Z'), 0, 56, 0, 0], [new Date('2019-06-16T12:00:00.000Z'), 3, 0, 0, 0]],
    hash = {},
    i, j,
    result,
    item,
    key;

for (i = 0; i < array.length; i++) {
    item = array[i];
    key = item[0].toString();
    if (!hash[key]) {
        hash[key] = item.slice();
        continue;
    }
    for (j = 1; j < item.length; j++) hash[key][j] += item[j];
}

result = Object.values(hash);

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

您可以使用 findIndex 检查累加器是否已经有新数组的 0 索引,如果有则合并数组:

const arr = [['2019-06-11T10:00:00.000Z', 0, 0, 0, 23], ['2019-06-11T10:00:00.000Z', 0, 0, 2, 0], ['2019-16-11T12:00:00.000Z', 0, 56, 0, 0], ['2019-16-11T12:00:00.000Z', 3, 0, 0, 0]]

const sorted = arr.reduce((acc, a) => {
  const index = acc.findIndex(b => a[0] === b[0])
  index > -1 ? acc[index] = acc[index].map((b, i) => i ? b + a[i]: b) : acc.push(a)
  return acc
}, [])

console.log(sorted)

好吧,在每个合并操作中需要考虑很多条件,但是您可以结合使用 Array.some(), Array.filter() and Array.map() 方法并采用如下方法:

let result = [];
array.forEach((a, i) => {
  if (!result.some(x => x[0] == a[0])) {
    let found = array.filter((x, j) => {
      return i != j && x[0] == a[0];
    });
    if (found.length > 0) {
      a = a.map((e, ind) => {
        if (ind > 0)
          found.forEach(f => {
            e += f[ind];
          });
        return e;
      });
    }
    result.push(a);
  }
});

演示:

var array = [
  [
    '2019-06-11T10:00:00.000Z',
    0,
    0,
    0,
    23
  ],
  [
    '2019-06-11T10:00:00.000Z',
    0,
    0,
    2,
    0
  ],
  [
    '2019-16-11T12:00:00.000Z',
    0,
    56,
    0,
    0
  ],
  [
    '2019-16-11T12:00:00.000Z',
    3,
    0,
    0,
    0
  ]
];

let result = [];

array.forEach((a, i) => {
  if (!result.some(x => x[0] == a[0])) {
    let found = array.filter((x, j) => {
      return i != j && x[0] == a[0];
    });
    if (found.length > 0) {
      a = a.map((e, ind) => {
        if (ind > 0)
          found.forEach(f => {
            e += f[ind];
          });
        return e;
      });
    }
    result.push(a);
  }
});
console.log(result);