-3

I have two arrays:

var test = [
  ["20180115", "0"],
  ["20180116", "0"],
  ["20180117", "0"],
  ["20180118", "0"],
  ["20180119", "0"],
  ["20180120", "0"],
  ["20180121", "0"]]

var test2 = [
  ["obj1", "20180115", "2"],
  ["obj2", "20180117", "8"], 
  ["obj3", "20180115", "1"],
  ["obj4", "20180118", "1"],
  ["obj5", "20180117", "3"]]

I want to format the test2 array to:

// sum the same day(or same string is ok) value and others add zero
var result = [
  ["20180115", "3"],   // 2+1
  ["20180116", "0"], 
  ["20180117", "11"],  // 8+3
  ["20180118", "1"],   // 1
  ["20180119", "0"],
  ["20180120", "0"],
  ["20180121", "0"]]

This is my current solution:

// sum the same day's data

let sumData = []
test.map(row => {
    const foundData = sumData.find(data => data[0] === row[1])
    let currentData = []
    if (foundData) {
        foundData[1] += row[2]
    } else {
        currentData = [row[1], parseInt(row[2], 10)]
        sumData.push(currentData)
    }
})

// use test2's date to formate new data

test = test2.map(row => {
    const foundData = sumData.find(data => data[0] === row[0])
    if (foundData) {
        row[1] = parseInt(row[1], 10) + parseInt(foundData[1], 10)
    }
    return row
})       

The code works but I don't think it's a very clean solution. Is there a better or cleaner way of achieving this?

KevinHu
  • 991
  • 3
  • 10
  • 20
  • 1
    `20180117` has two `obj` with different names. what is the rule of the result to add the values, but take `obj3` as result? – Nina Scholz Jan 22 '18 at 07:24
  • 1
    [Code Review](https://codereview.stackexchange.com/) ? – Kos Jan 22 '18 at 07:26
  • sorry, I edit it again. obj1 ~ obj5 is different data. – KevinHu Jan 22 '18 at 07:43
  • I just want to know is there any better solution :( – KevinHu Jan 22 '18 at 07:44
  • why on `20180117` `'obj3'` and not `'obj5'`? just the first one or the smallest or ...? – Nina Scholz Jan 22 '18 at 07:52
  • Sorry for that again, I think I can ignore the obj id, just need the date and value. :) – KevinHu Jan 22 '18 at 08:03
  • I'm voting to close this question as off-topic because it belongs on https://codereview.stackexchange.com/ (not SoftwareRecs) – Wonko the Sane Jan 22 '18 at 15:58
  • @user8371915 I don't know what you've been smoking, but that's definitely not the right place for this question. – Mast Jan 22 '18 at 16:09
  • @KevinHu this is essentially a [duplicate of your own question](https://stackoverflow.com/q/48110240/633183) – there, the items to combine are objects, but here the items are arrays; it makes no difference in the end. the technique to combine them is the same ^_^ – Mulan Jan 22 '18 at 16:28

1 Answers1

0

You could take a Map to have a faster access tp the array with the same date

var test = [["20180115", "0"], ["20180116", "0"], ["20180117", "0"], ["20180118", "0"], ["20180119", "0"], ["20180120", "0"], ["20180121", "0"]],
    test2 = [["obj1", "20180115", "2"], ["obj2", "20180117", "8"], ["obj3", "20180115", "1"], ["obj4", "20180118", "1"], ["obj5", "20180117", "3"]],
    map = new Map();
    result = test.map(([d, v]) => map.set(d, [d, v]).get(d));

test2.forEach(([, d, v]) => map.get(d)[1] = (+map.get(d)[1] + +v).toString());

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392