js兩個(gè)數(shù)組根據(jù)對象中某一相同屬性,計(jì)算合并其他項(xiàng),無相同不計(jì)算

image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr1 = [{
                YearMonth: "2021-08",
                PassNum: 20,
                NotPassNum: 5,
                AllNum: 25
            },
            // {
            //     YearMonth: "2021-09",
            //     PassNum: 21,
            //     NotPassNum: 0,
            //     AllNum: 21
            // },
            {
                YearMonth: "2021-10",
                PassNum: 16,
                NotPassNum: 1,
                AllNum: 17
            }, {
                YearMonth: "2021-11",
                PassNum: 10,
                NotPassNum: 0,
                AllNum: 11
            }
        ]
        var arr2 = [{
            YearMonth: "2021-08",
            PassNum: 30,
            NotPassNum: 5,
            AllNum: 35
        }, {
            YearMonth: "2021-09",
            PassNum: 41,
            NotPassNum: 0,
            AllNum: 41
        }, {
            YearMonth: "2021-10",
            PassNum: 45,
            NotPassNum: 1,
            AllNum: 46
        }, {
            YearMonth: "2021-11",
            PassNum: 80,
            NotPassNum: 1,
            AllNum: 81
        }]
        var newobj = []

        var arry = arr1.concat(arr2);


        let newfood = [];
        var temp = {};
        for (var i in arry) {
            var key = arry[i].YearMonth;
            if (temp[key]) {
                temp[key].YearMonth = temp[key].YearMonth;
                temp[key].PassNum = temp[key].PassNum + arry[i].PassNum;
                temp[key].NotPassNum = temp[key].NotPassNum + arry[i].NotPassNum;
                temp[key].AllNum = temp[key].AllNum + arry[i].AllNum;

            } else {
                temp[key] = {};
                temp[key].YearMonth = arry[i].YearMonth;
                temp[key].PassNum = arry[i].PassNum;
                temp[key].NotPassNum = arry[i].NotPassNum;
                temp[key].AllNum = arry[i].AllNum;

            }
            // temp[key].ItemType= arry[i].ItemType; 
        }
        for (var k in temp) {
            newfood.push(temp[k])
        }



        function dateDate(property, bol) {
            return function(a, b) {
                var value1 = a[property];
                var value2 = b[property];
                if (bol) {
                    return Date.parse(value1) - Date.parse(value2);
                } else {
                    return Date.parse(value2) - Date.parse(value1);
                }
            };
        }

        var ddd = newfood.sort(dateDate("YearMonth", true))

    </script>
</body>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容