根据数组中的字段,插入相应数据

  // 有以下数组arr
      let arr = [
        {
          dt: "2021-01",
          org_name: "雪肌",
          ordercount: 6860,
          amountsum: 5279333.89,
        },
        {
          dt: "2021-01",
          org_name: "智美",
          ordercount: 315,
          amountsum: 1632081.63,
        },
        {
          dt: "2021-01",
          org_name: "易购",
          ordercount: 19267,
          amountsum: 576536.02,
        },
        {
          dt: "2021-01",
          org_name: "新云商",
          ordercount: 7937,
          amountsum: 383853.12,
        },
        {
          dt: "2021-02",
          org_name: "雪肌",
          ordercount: 5923,
          amountsum: 5337823.19,
        },
        {
          dt: "2021-02",
          org_name: "智美",
          ordercount: 143,
          amountsum: 1444326.27,
        },
        {
          dt: "2021-02",
          org_name: "新云商",
          ordercount: 7679,
          amountsum: 395459.04,
        },
        {
          dt: "2021-02",
          org_name: "易购",
          ordercount: 2959,
          amountsum: 164336.91,
        },
        {
          dt: "2021-03",
          org_name: "智美",
          ordercount: 462,
          amountsum: 14206588.23,
        },
        {
          dt: "2021-03",
          org_name: "雪肌",
          ordercount: 5755,
          amountsum: 6186629.37,
        },
        {
          dt: "2021-03",
          org_name: "易购",
          ordercount: 18956,
          amountsum: 749612.85,
        },
        {
          dt: "2021-03",
          org_name: "新云商",
          ordercount: 4835,
          amountsum: 225894.92,
        },
        { dt: "2021-03", org_name: "雪莉", ordercount: 2, amountsum: 30083 },
        {
          dt: "2021-04",
          org_name: "雪肌",
          ordercount: 7576,
          amountsum: 4810868.25,
        },
        {
          dt: "2021-04",
          org_name: "智美",
          ordercount: 402,
          amountsum: 2199224.42,
        },
        {
          dt: "2021-04",
          org_name: "雪莉",
          ordercount: 23,
          amountsum: 1240743.64,
        },
        {
          dt: "2021-04",
          org_name: "易购",
          ordercount: 8137,
          amountsum: 509817.85,
        },
        {
          dt: "2021-04",
          org_name: "新云商",
          ordercount: 3146,
          amountsum: 129066.09,
        },
        {
          dt: "2021-05",
          org_name: "雪肌",
          ordercount: 1979,
          amountsum: 1443072.02,
        },
        {
          dt: "2021-05",
          org_name: "智美",
          ordercount: 98,
          amountsum: 1173696.93,
        },
        {
          dt: "2021-05",
          org_name: "易购",
          ordercount: 5841,
          amountsum: 216819.41,
        },
        { dt: "2021-05", org_name: "雪莉", ordercount: 3, amountsum: 120986 },
        {
          dt: "2021-05",
          org_name: "新云商",
          ordercount: 1705,
          amountsum: 62176.13,
        },
      ];
      // 要得到以下数组
      newArr = [
        {
          dt: "2021-01",
          org_name: "雪莉",
          ordercount: 0,
          amountsum: 0,
        },
        {
          dt: "2021-01",
          org_name: "雪肌",
          ordercount: 6860,
          amountsum: 5279333.89,
        },
        {
          dt: "2021-01",
          org_name: "智美",
          ordercount: 315,
          amountsum: 1632081.63,
        },
        {
          dt: "2021-01",
          org_name: "易购",
          ordercount: 19267,
          amountsum: 576536.02,
        },
        {
          dt: "2021-01",
          org_name: "新云商",
          ordercount: 7937,
          amountsum: 383853.12,
        },
        {
          dt: "2021-02",
          org_name: "雪莉",
          ordercount: 0,
          amountsum: 0,
        },
        {
          dt: "2021-02",
          org_name: "雪肌",
          ordercount: 5923,
          amountsum: 5337823.19,
        },
        {
          dt: "2021-02",
          org_name: "智美",
          ordercount: 143,
          amountsum: 1444326.27,
        },
        {
          dt: "2021-02",
          org_name: "新云商",
          ordercount: 7679,
          amountsum: 395459.04,
        },
        {
          dt: "2021-02",
          org_name: "易购",
          ordercount: 2959,
          amountsum: 164336.91,
        },
        {
          dt: "2021-03",
          org_name: "智美",
          ordercount: 462,
          amountsum: 14206588.23,
        },
        {
          dt: "2021-03",
          org_name: "雪肌",
          ordercount: 5755,
          amountsum: 6186629.37,
        },
        {
          dt: "2021-03",
          org_name: "易购",
          ordercount: 18956,
          amountsum: 749612.85,
        },
        {
          dt: "2021-03",
          org_name: "新云商",
          ordercount: 4835,
          amountsum: 225894.92,
        },
        { dt: "2021-03", org_name: "雪莉", ordercount: 2, amountsum: 30083 },
        {
          dt: "2021-04",
          org_name: "雪肌",
          ordercount: 7576,
          amountsum: 4810868.25,
        },
        {
          dt: "2021-04",
          org_name: "智美",
          ordercount: 402,
          amountsum: 2199224.42,
        },
        {
          dt: "2021-04",
          org_name: "雪莉",
          ordercount: 23,
          amountsum: 1240743.64,
        },
        {
          dt: "2021-04",
          org_name: "易购",
          ordercount: 8137,
          amountsum: 509817.85,
        },
        {
          dt: "2021-04",
          org_name: "新云商",
          ordercount: 3146,
          amountsum: 129066.09,
        },
        {
          dt: "2021-05",
          org_name: "雪肌",
          ordercount: 1979,
          amountsum: 1443072.02,
        },
        {
          dt: "2021-05",
          org_name: "智美",
          ordercount: 98,
          amountsum: 1173696.93,
        },
        {
          dt: "2021-05",
          org_name: "易购",
          ordercount: 5841,
          amountsum: 216819.41,
        },
        { dt: "2021-05", org_name: "雪莉", ordercount: 3, amountsum: 120986 },
        {
          dt: "2021-05",
          org_name: "新云商",
          ordercount: 1705,
          amountsum: 62176.13,
        },
      ];
阅读 1.4k
2 个回答

虽然这么多人吐槽,但是我还是想吃分啊。果然还是吃分快乐。(mac 的快捷键功能能不能给加一下,它没有 ctrl


我们先来分析,把题主提供的数据粘贴到控制台中。然后输出一下,哎呦,新数组多数据了(圈住,要考)

image.png

接下来,我们点开看看能不能找到规律

image.png

规律还可以。就是把每天没出售的商品补全,那我们先来搞一下都有哪些商品 Array.from(new Set(arr.map(v=>v.org_name))),同理得出有多少天

image.png

接下来就是遍历填充数据了。

image.png

result = [];
Array.from(new Set(arr.map(v=>v.dt))).forEach((dt)=>{
    Array.from(new Set(arr.map(v=>v.org_name))).forEach((org_name)=>{
        let item = arr.find(v=>v.dt == dt && v.org_name == org_name)
        if(!item) item = {dt,org_name,ordercount: 0,amountsum: 0}
        result.push(item);
    })
})
result

好了问题已经解决了。还是建议你下次提问要把问题描述的更清晰一点
答题也就只能得一点分而已,又不是得到钱了。(社区大佬们都是日薪过千,时薪过百,那算下来一分钟好几块钱呢,你说说答一道描述不清的题浪费多少时间和金钱

@linong 回答了,来作个补充。

主要是发现 @linong 有一个小小的漏洞,虽然不太可能发生:最终得到的日期列表,有可能并不是连续的,需要补足


因为得到的日期(月)集不一定是连续的,所以需要判断。这时候要考虑几个问题:

  1. Array.from(new Set(...)) 输出的日期不一定是从小到大的顺序(虽然一般来说是)
  2. 月到年是 12 进制,不是 10 进制,不能简单的去掉 - 号再 parseInt() 来计算

所以,先把日期算成数值,用 年 * 12 + (月 - 1)

function toInteger(dt) {
    const [y, m] = dt.split("-").map(s => parseInt(s, 10));
    return y * 12 + (m - 1);
}

然后获得日期集合

let dtList = Array.from(new Set(arr.map(it => it.dt))).sort();

为了试验中间缺日期的情况,可以手工删除掉一些,比如

dtList.splice(1, 2);    // DEBUG 移除中间一些
console.log(dtList);    // [ '2021-01', '2021-04', '2021-05' ]

取最小最大值

const [min, max] = (() => {
    const il = dtList.map(toInteger);
    return [il[0], il[il.length - 1]];
})();

如果 max - min + 1dtList.length 相等,那说明不缺数据,否则

if (max - min + 1 > dtList.length) {
    const size = max - min + 1;
    // 如果中间缺日期,需要重新产生 dtList
    dtList = Array
        .from(
            Array(size),
            (_, i) => i + min   // 产生从 min 开始顺序 + 1 的序列
        )
        .map(n => [
            Math.floor(n / 12),                         // 取年
            (n % 12 + 1).toString().padStart(2, "0")    // 取月
        ].join("-"));           // 合成日期字符串
}

最后一般来说,为了避免不小心修改到 dtList,把它用 IIFE 封装一下

const dtList = (() => {
    function toInteger(dt) {
        const [y, m] = dt.split("-").map(s => parseInt(s, 10));
        return y * 12 + (m - 1);
    }

    const dtList = Array.from(new Set(arr.map(it => it.dt))).sort();

    const [min, max] = (() => {
        const il = dtList.map(toInteger);
        return [il[0], il[il.length - 1]];
    })();

    if (max - min + 1 > dtList.length) {
        return dtList;
    }

    // 前面发现没有缺数据,直接返回 dtList,后来来重新生成

    const size = max - min + 1;
    return Array
        .from(Array(size), (_, i) => i + min)
        .map(
            n => [
                Math.floor(n / 12),
                (n % 12 + 1).toString().padStart(2, "0")
            ].join("-")
        );
})();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题