简体   繁体   English

在 Javascript 的多维数组中合并具有相同日期的值

[英]Combine value with same date in multidimensional array in Javascript

I have one array which has two values one date and an amount but there is the same date with a different value at a different index so I need to merge those values into the same date in a multidimensional javascript array我有一个数组,它有两个值,一个日期和一个金额,但是在不同的索引处有相同的日期和不同的值,所以我需要将这些值合并到多维 javascript 数组中的相同日期

current array当前数组

var arry = [
['2021-05-01',100], 
['2021-05-02',300], 
['2021-05-03',200], 
['2021-05-01',150],
['2021-05-02',300], 
['2021-05-01',600],
['2021-05-04',120]
]

Expected Result Array预期结果数组

var arry = [
['2021-05-01',850], 
['2021-05-02',600], 
['2021-05-03',200], 
['2021-05-04',120]]

Can anybody help?有人可以帮忙吗? I really appreciate any help you can provide.我真的很感激你能提供的任何帮助。

You can use reduce method:您可以使用reduce方法:

 var arry = [ ['2021-05-01',100], ['2021-05-02',300], ['2021-05-03',200], ['2021-05-01',150], ['2021-05-02',300], ['2021-05-01',600], ['2021-05-04',120] ]; const data = arry.reduce((list, [date, value]) => { list[date] = (list[date]?? 0) + value; return list; }, {}); const list = Object.entries(data); // if you want the array console.log(list);

Here is a solution using reduce .这是使用reduce的解决方案。 I'm creating an object grouped by date and taking the values array of it我正在创建一个按日期分组的 object 并获取它的值数组

 var arry = [['2021-05-01',100], ['2021-05-02',300], ['2021-05-03',200], ['2021-05-01',150],['2021-05-02',300], ['2021-05-01',600],['2021-05-04',120]] const res = Object.values(arry.reduce((acc,[date,val])=> { acc[date] = acc[date] || [date,0] acc[date][1]+=val return acc },{})) console.log(res)

You could likely just loop through the list, and at each value in the list check all of the values that came before to see if it is a repeat.您可能只是循环遍历列表,并在列表中的每个值处检查之前出现的所有值以查看它是否重复。 Something like the following would work:像下面这样的东西会起作用:

var array = [
  ['2021-05-01',100], 
  ['2021-05-02',300], 
  ['2021-05-03',200], 
  ['2021-05-01',150],
  ['2021-05-02',300], 
  ['2021-05-01',600],
  ['2021-05-04',120]
]

var newArray = []; // where we will put the new merged values

for (var lookAtOldArray = 0; lookAtOldArray < array.length; lookAtOldArray++) {
  // check all previous values in newArray up to this point:
  var checkNewArray; // declare out here for logic post loop
  for (checkNewArray = 0; checkNewArray < newArray.length; checkNewArray++) {
    if (array[lookAtOldArray][0] == newArray[checkNewArray][0]) {
      // add to newArray number:
      newArray[checkNewArray][1] += array[lookAtOldArray][1];
      break; // BREAK to signify we found a position
    }

    // otherwise keep looking
  }

  // because of the break, if checkNewArray == newArray.length, then we DID NOT find a duplicate (AKA: push to newArray with exact value in array)
  if (checkNewArray == newArray.length) {
     newArray.push(array[lookAtOldArray]);
  }
}

another interesting idea would be to use objects.另一个有趣的想法是使用对象。 The above strategy would require looking the data more times than necessary (n^2 for worst cases).上述策略需要查看数据的次数超过必要次数(最坏情况下为 n^2)。 With an object, you could use the date as a key, and the number as the value.对于 object,您可以使用日期作为键,使用数字作为值。 With that, you could do something like this (assuming the same declaration of array :有了它,你可以做这样的事情(假设array的相同声明:

var combinedDates = {};

for (var lookAtOldArray = 0; lookAtOldArray < array.length; lookAtOldArray++) {
  combinedDates[array[lookAtOldArray][0]] += array[lookAtOldArray[1];
}

You can tell this is a lot faster and simpler.您可以看出这更快更简单。 The resulting object would look like this:生成的 object 如下所示:

{
  "2021-05-01": 850,
  "2021-05-02": 600,
  "2021-05-03": 200,
  "2021-05-04": 120
}

I'm preferential to the second one because of the efficiency increase;由于效率提高,我更喜欢第二个; you only ever have to look at each element in the initial array once (resulting in n , or linear, time complexity)!您只需查看初始数组中的每个元素一次(导致n或线性时间复杂度)!

If you want to combine the above object back into an array, you could then do:如果你想将上面的 object 组合回一个数组,你可以这样做:

// get object keys
var objectKeys = Object.keys(combinedDates);
var newArray = [];

// loop through keys
for (var makeObject = 0; makeObject < objectKeys.length; makeObject++) {
  // create array with key (the date) and the number (the value stored at the key):
  newArray.push([objectKeys[makeObject][0], combinedDates[objectKeys[makeObject]]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM