Home > Blockchain >  Compare 2 arrays and combine in Javascript
Compare 2 arrays and combine in Javascript

Time:01-11

How can i compare and combine 2 arrays using array functions of javascript or using lodash?

I have this initial array of dates for last 30 days.

[
  '2022-12-11', '2022-12-12', '2022-12-13',
  '2022-12-14', '2022-12-15', '2022-12-16',
  '2022-12-17', '2022-12-18', '2022-12-19',
  '2022-12-20', '2022-12-21', '2022-12-22',
  '2022-12-23', '2022-12-24', '2022-12-25',
  '2022-12-26', '2022-12-27', '2022-12-28',
  '2022-12-29', '2022-12-30', '2022-12-31',
  '2023-01-01', '2023-01-02', '2023-01-03',
  '2023-01-04', '2023-01-05', '2023-01-06',
  '2023-01-07', '2023-01-08', '2023-01-09',
  '2023-01-10', '2023-01-11'
]

Then this is the second with count value.

[ [ '2023-01-09', 1 ], [ '2023-01-10', 3 ] ]

Now i have this code that compare and combine these array manually

        let testData = [];
        let k = 0;
        dayList.forEach(o => {
            let is_match = 0;
            let frags = [];
            submitted.forEach(i => {
                if(o == i[0]){
                    is_match = 1;
                    frags = i;
                }
            });

            testData[k] = [
                (is_match == 1) ? frags[0] : o,
                (is_match == 1) ? frags[1] : 0
            ];

            k  ;
        });

        console.log(testData);

this will result to...

[
  [ '2022-12-11', 0 ], [ '2022-12-12', 0 ],
  [ '2022-12-13', 0 ], [ '2022-12-14', 0 ],
  [ '2022-12-15', 0 ], [ '2022-12-16', 0 ],
  [ '2022-12-17', 0 ], [ '2022-12-18', 0 ],
  [ '2022-12-19', 0 ], [ '2022-12-20', 0 ],
  [ '2022-12-21', 0 ], [ '2022-12-22', 0 ],
  [ '2022-12-23', 0 ], [ '2022-12-24', 0 ],
  [ '2022-12-25', 0 ], [ '2022-12-26', 0 ],
  [ '2022-12-27', 0 ], [ '2022-12-28', 0 ],
  [ '2022-12-29', 0 ], [ '2022-12-30', 0 ],
  [ '2022-12-31', 0 ], [ '2023-01-01', 0 ],
  [ '2023-01-02', 0 ], [ '2023-01-03', 0 ],
  [ '2023-01-04', 0 ], [ '2023-01-05', 0 ],
  [ '2023-01-06', 0 ], [ '2023-01-07', 0 ],
  [ '2023-01-08', 0 ], [ '2023-01-09', 1 ],
  [ '2023-01-10', 3 ], [ '2023-01-11', 0 ]
]

As you can see the date 2023-01-09 and 2023-01-10 have values then the rest has 0 values.

Which is what i expected, i'm just new in coding a pure javascript application, i just translated my PHP code to javascript.

Now is there a way that this code may be simplified using array functions of javascript or using lodash?

CodePudding user response:

You can simply run any loop and find the index of the current element in the submitted array and check if exist then assign date otherwise assign 0 to the array

var dayList = ['2022-12-11', '2023-01-10', '2023-01-09']
var submitted = [ [ '2023-01-09', 1 ], [ '2023-01-10', 3 ] ]
var testData = []
dayList.filter(o => {
    const exist = submitted.find(e => e.indexOf(o) != -1)
    if(exist){
        testData.push([o, exist[1]])
    } else {
        testData.push([o, 0])
    }
});
console.log("your data=", testData)

CodePudding user response:

Here's an approach, we first create a map with the date as key and count as value and then use this map to generate the result

const dates=["2022-12-11","2022-12-12","2022-12-13","2022-12-14","2022-12-15","2022-12-16","2022-12-17","2022-12-18","2022-12-19","2022-12-20","2022-12-21","2022-12-22","2022-12-23","2022-12-24","2022-12-25","2022-12-26","2022-12-27","2022-12-28","2022-12-29","2022-12-30","2022-12-31","2023-01-01","2023-01-02","2023-01-03","2023-01-04","2023-01-05","2023-01-06","2023-01-07","2023-01-08","2023-01-09","2023-01-10","2023-01-11",];

const count=[["2023-01-09",1],["2023-01-10",3]];

const countMap = count.reduce((acc, [date, count]) => {
    acc[date] = count;
    return acc;
}, {});

const result = dates.map((date) => [date, countMap[date] || 0]);

console.log(result)

  • Related