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

时间:2019-07-15 07:28:48

标签: javascript arrays duplicates array-merge

我有一个数组数组,其日期为索引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

3 个答案:

答案 0 :(得分:1)

您可以使用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)

答案 1 :(得分:0)

使用对象的快速方法。

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; }

答案 2 :(得分:0)

在每个合并操作中都需要考虑很多条件,但是您可以使用Array.some()Array.filter()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);