比较2个数组并在Javascript中合并

wr98u20j  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(141)

我如何使用javascript的数组函数或使用lodash比较和合并2个数组?
我有过去30天的初始日期数组。

[
  '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'
]

这是第二个计数值。

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

现在,我有了手动比较和合并这些数组的代码

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

这将导致...

[
  [ '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 ]
]

如您所见,日期2023-01-092023-01-10具有值,其余日期具有0值。
这是我所期望的,我只是一个新的编码纯javascript应用程序,我只是把我的PHP代码转换成javascript。
现在有没有一种方法可以简化这段代码,使用javascript的数组函数或者使用lodash?

0x6upsns

0x6upsns1#

这里有一种方法,我们首先创建一个Map,将日期作为键,将计数作为值,然后使用此Map生成结果

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)
mwyxok5s

mwyxok5s2#

您可以简单地运行任何循环,在提交的数组中查找当前元素的索引,如果存在,则为数组赋值date,否则为数组赋值0

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)

相关问题