SOURCE


const verifyKeys = [
    {
        name: '当前盘点批次',
        key: 'inventoryID',
        isRequired: true,
        isOnly: false
    },
    {
        name: '盘点状态',
        key: 'inventoryStatus',
        isRequired: true,
        isOnly: false
    },
    {
        name: '设备状态(盘点前)',
        key: 'status',
        isRequired: true,
        isOnly: false
    },
    {
        name: '设备状态(盘点后)',
        key: 'newDeviceStatus',
        isRequired: false,
        isOnly: false
    },
    {
        name: '用户工号',
        key: 'userID',
        isRequired: false,
        isOnly: false
    },
    {
        name: '用户姓名',
        key: 'userName',
        isRequired: false,
        isOnly: false
    },
    {
        name: '盘点时间',
        key: 'actionAt',
        isRequired: false,
        isOnly: false,
        isTime: true
    },
    {
        name: '盘点行为',
        key: 'actionType',
        isRequired: true,
        isOnly: false
    },
    {
        name: '盘点结果',
        key: 'inventoryResult',
        isRequired: true,
        isOnly: false
    },
    {
        name: 'OA备注',
        key: 'inventoryNote',
        isRequired: false,
        isOnly: false
    },
    {
        name: '用户备注',
        key: 'userNote',
        isRequired: false,
        isOnly: false
    },
    {
        name: '盘点异常原因',
        key: 'inventoryaAnromal',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'SerialNumber',
        key: 'serialnumber',
        isRequired: true,
        isOnly: false
    },
    {
        name: 'AssetTag',
        key: 'assettag',
        isRequired: true,
        isOnly: false
    },
    {
        name: 'Class',
        key: 'class',
        isRequired: true,
        isOnly: false,
        isSpecial: true
    },
    {
        name: 'Type',
        key: 'type',
        isRequired: true,
        isOnly: false,
        isSpecial: true
    },
    {
        name: 'BU_Stockroom',
        key: 'bu_stockroom',
        isRequired: true,
        isOnly: false
    },
    {
        name: 'Model',
        key: 'model',
        isRequired: false,
        isOnly: false,
        isSpecial: true
    },
    {
        name: 'Config',
        key: 'config',
        isRequired: false,
        isOnly: false,
        isSpecial: true
    },
    {
        name: 'BU_SAP',
        key: 'bu_sap',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'SAPNumber',
        key: 'sapnumber',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'RITMNumber',
        key: 'ritmnumber',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'ProcurementPONumber',
        key: 'procurementponumber',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'ExpirationDate',
        key: 'expirationdate',
        isRequired: false,
        isOnly: false,
        isTime: true
    },
    {
        name: 'PhoneNumber',
        key: 'phonenumber',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'UserType',
        key: 'usertype',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'Action',
        key: 'action',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'ExpressNumber',
        key: 'expressnumber',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'ApproveID',
        key: 'approveid',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'EsignID',
        key: 'esignid',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'DeliveryDate',
        key: 'releasedate',
        isRequired: false,
        isOnly: false,
        isTime: true
    },
    {
        name: 'ReturnDate',
        key: 'returnDate',
        isRequired: false,
        isOnly: false,
        isTime: true
    },
    {
        name: 'ExpectedReturnDate',
        key: 'expectedreturndate',
        isRequired: false,
        isOnly: false,
        isTime: true
    },
    {
        name: 'PICID',
        key: 'ownerid',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'PICName',
        key: 'ownername',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'PICDepartment',
        key: 'ownerdepartment',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'OwnerID',
        key: 'picid',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'OwnerName',
        key: 'picname',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'OwnerDepartment',
        key: 'picdepartment',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'Price',
        key: 'price',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'Abnormal',
        key: 'abnormal',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'Comment',
        key: 'comment',
        isRequired: false,
        isOnly: false
    },
    {
        name: 'Apple类型',
        key: 'appleType',
        isRequired: false,
        isOnly: false
    }
]

const verifyArr = [

    [

        { r: '0', c: '0', v: '当前盘点批次' },

        { r: '0', c: '1', v: 'AssetTag' },

        { r: '0', c: '2', v: 'SerialNumber' },

        { r: '0', c: '3', v: '盘点状态' },

        { r: '0', c: '4', v: '设备状态(盘点前)' },

        { r: '0', c: '5', v: '设备状态(盘点后)' },

        { r: '0', c: '6', v: '用户工号' },

        { r: '0', c: '7', v: '用户姓名' },

        { r: '0', c: '8', v: '盘点时间' },

        { r: '0', c: '9', v: '盘点行为' },

        { r: '0', c: '10', v: '盘点结果' },

        { r: '0', c: '11', v: 'OA备注' },

        { r: '0', c: '12', v: '用户备注' },

        { r: '0', c: '13', v: '盘点异常原因' },

        { r: '0', c: '14', v: 'Class' },

        { r: '0', c: '15', v: 'Type' },

        { r: '0', c: '16', v: 'Model' },

        { r: '0', c: '17', v: 'Config' },

        { r: '0', c: '18', v: 'Apple类型' },

        { r: '0', c: '19', v: 'SAPNumber' },

        { r: '0', c: '20', v: 'RITMNumber' },

        { r: '0', c: '21', v: 'ProcurementPONumber' },

        { r: '0', c: '22', v: 'ExpirationDate' },

        { r: '0', c: '23', v: 'PhoneNumber' },

        { r: '0', c: '24', v: 'BU_Stockroom' },

        { r: '0', c: '25', v: 'BU_SAP' },

        { r: '0', c: '26', v: 'UserType' },

        { r: '0', c: '27', v: 'Action' },

        { r: '0', c: '28', v: 'ExpressNumber' },

        { r: '0', c: '29', v: 'ApproveID' },

        { r: '0', c: '30', v: 'DeliveryDate' },

        { r: '0', c: '31', v: 'ExpectedReturnDate' },

        { r: '0', c: '32', v: 'EsignID' },

        { r: '0', c: '33', v: 'PICID' },

        { r: '0', c: '34', v: 'PICName' },

        { r: '0', c: '35', v: 'PICDepartment' },

        { r: '0', c: '36', v: 'OwnerID' },

        { r: '0', c: '37', v: 'OwnerName' },

        { r: '0', c: '38', v: 'OwnerDepartment' },

        { r: '0', c: '39', v: 'ReturnDate' },

        { r: '0', c: '40', v: 'Price' },

        { r: '0', c: '41', v: 'Comment' },

        { r: '0', c: '42', v: 'Abnormal' }

    ],

    [

        { r: '1', c: '0', v: '20231208192625-0' },

        { r: '1', c: '1', v: 'test11001' },

        { r: '1', c: '2', v: 'inventoryTest001' },

        { r: '1', c: '3', v: '0' },

        { r: '1', c: '4', v: '在库' },

        { r: '1', c: '5', v: '使用中' },

        { r: '1', c: '6', v: '' },

        { r: '1', c: '7', v: '' },

        { r: '1', c: '8', v: '' },

        { r: '1', c: '9', v: '0' },

        { r: '1', c: '10', v: '0' },

        { r: '1', c: '11', v: '' },

        { r: '1', c: '12', v: '' },

        { r: '1', c: '13', v: '' },

        { r: '1', c: '14', v: '移动设备1' },

        { r: '1', c: '15', v: 'Apple' },

        { r: '1', c: '16', v: 'iPhone 14' },

        { r: '1', c: '17', v: '' },

        { r: '1', c: '18', v: '' },

        { r: '1', c: '19', v: '' },

        { r: '1', c: '20', v: '' },

        { r: '1', c: '21', v: '' },

        { r: '1', c: '22', v: '' },

        { r: '1', c: '23', v: '' },

        { r: '1', c: '24', v: 'FRCN-HQ' },

        { r: '1', c: '25', v: '' },

        { r: '1', c: '26', v: '' },

        { r: '1', c: '27', v: '' },

        { r: '1', c: '28', v: '' },

        { r: '1', c: '29', v: '' },

        { r: '1', c: '30', v: '' },

        { r: '1', c: '31', v: '' },

        { r: '1', c: '32', v: '' },

        { r: '1', c: '33', v: '' },

        { r: '1', c: '34', v: '' },

        { r: '1', c: '35', v: '' },

        { r: '1', c: '36', v: '' },

        { r: '1', c: '37', v: '' },

        { r: '1', c: '38', v: '' },

        { r: '1', c: '39', v: '' },

        { r: '1', c: '40', v: '' },

        { r: '1', c: '41', v: '' },

        { r: '1', c: '42', v: '' }

    ]

]

const data = { "class": ["电脑", "移动设备", "固定资产", "软件", ""], "type": ["Apple", "DELL", "显示器", "手机号", "电话机", "音响", "打印机", "投影仪", "maxhub", "许可证", ""], "model": ["iMac", "MacBook air", "MacBook Pro", "Dell Inc. Latitude 5300", "Dell Inc. Latitude 5301", "Dell Inc. Latitude 5310", "Dell Inc. Latitude 5311", "Dell Inc. Latitude 5320", "Dell Inc. Latitude 5321", "Dell Inc. Latitude 5330", "Dell Inc. Latitude 5331", "Dell Inc. Latitude 5340", "Dell Inc. Latitude 5341", "Dell Inc. Latitude 5430", "Dell Inc. Latitude 5440", "Dell Inc. Latitude 7280", "Dell Inc. Latitude 7290", "Dell Inc. Latitude E7240", "Dell Inc. Latitude E7250", "Dell Inc. Latitude E7270", "Dell Inc. OptiPlex 5040", "Dell Inc. OptiPlex 5050", "Dell Inc. OptiPlex 5080", "Dell Inc. OptiPlex 5000", "Dell Inc. Precision 5510", "Dell Inc. Precision 5520", "Dell Inc. Precision 5540", "Dell Inc. Precision 5550", "Dell Inc. Precision 5560", "Dell Inc. Precision 7530", "Dell Inc. Precision 7670", "Dell Inc. XPS 13", null, "iPhone 8", "iPhone SE(第二代)", "iPhone XR", "iPhone 11", "iPhone 12", "iPhone 13", "iPAD", "iPad mini", "iPad Pro", "iTouch", "AppleTV", "Apple Studio Display", ""], "config": ["27 inch 512GB固态硬盘 16GB内存", "14 inch,CPU M2 8核,GPU 14核,硬盘 512GB,内存 16GB", "14 inch,CPU M1 8核,GPU 14核,硬盘 512GB,内存 16GB", "14 inch,CPU M2 Pro 10核,GPU 14核,硬盘 512GB,内存 16GB", "14 inch,CPU M2 Pro 8核,GPU 14核,硬盘 512GB,内存 16GB", "14 inch,CPU M2 Pro 10核,GPU 14核,硬盘 512GB,内存 32GB", "16 inch,CPU M2 Pro 10核,GPU 16核,硬盘 512GB,内存 16GB", "CPU i5,硬盘 256GB,内存 16GB", "CPU i5,硬盘 512GB,内存 16GB", "CPU i9,硬盘 512GB,内存 64GB", "CPU i7,硬盘 256GB,内存16GB", "24寸显示器", "27寸显示器", "34寸显示器", "内存 64GB", "内存 256GB", "内存 128GB", "Wi-Fi版,内存 64GB", "Wi-Fi+蜂窝,内存64GB", "Wi-Fi版,内存 128GB", "Wi-Fi+蜂窝,内存128GB", "11寸,Wi-Fi版,内存 512GB", "11寸,Wi-Fi版,内存 1TB", "12寸,Wi-Fi版,内存 512GB", "12寸,Wi-Fi版,内存1TB", null, "Avaya IP话机", "5代机,Windows,55寸", "5代机,Windows,65寸", "5代机,Windows,75寸", "5代机,Windows,86寸", "6代机,Windows,98寸", ""] }

// 特殊字段校验
function verifySpecial(verifyArr, verifyKeys, data) {

    // 获取需要校验的特殊校验集合
    const verifyObj = data

    let xlsxErr = []

    // 检索出特殊字段对象
    let requiredArr = []
    verifyKeys.map(item => {
        if (item.isSpecial) {
            requiredArr.push(item)
        }
    })

    // 获取标题列
    let titles = verifyArr[0]

    // 创建一个映射对象,将 array2 中的 v 与 array1 中的 name 对应起来
    const mapping = {};
    titles.forEach(item => {
        mapping[item.v] = item.c;
    });

    // 遍历 array1,如果 name 在映射中有对应的值,则赋值
    requiredArr.forEach(item => {
        if (mapping[item.name] !== undefined) {
            item.c = mapping[item.name];
        }
    });

    /*
       获得 requiredArr 此时案例为:[{"name":"Class","key":"class","isRequired":true,"isOnly":false,"isSpecial":true,"c":"14"}, ...]
    */

    // 合并特殊字段要检查的值
    requiredArr.map(item => {
        const key = item.key
        item.verifys = verifyObj[key]
    })

    /*
      获得 requiredArr 此时案例为:
      [{"name":"Class","key":"class","isRequired":true,"isOnly":false,"isSpecial":true,"c":"14", "verifys":['电脑', '移动设备',...]}, ...]
    */

    // verifyArr 内部均为数组,使之进行合并
    let newVerifyArr = []
    verifyArr = verifyArr.map(item => {
        newVerifyArr = newVerifyArr.concat(item)
    })

    // console.log(JSON.stringify(newVerifyArr))

    // 进行排查,不是必填字段列的跳过本次循环,减少计算时间
    for (let i = 0; i < newVerifyArr.length; i++) {
        const item = newVerifyArr[i]

        // 标题列直接跳过
        if (item.r === "0") {
            continue;
        }

        for (let j of requiredArr) {
            if (j.c === item.c) {
                // 判断特殊字段是否符合
                if (!j.verifys.includes(item.v)) {
                    const row = (+item.r + 1)
                    xlsxErr.push('第' + row + `行的${j.key}:` + item.v + ` 在数据库中不存在!`)
                }
                continue;
            }
        }
    }

    return xlsxErr

}

const result = verifySpecial(verifyArr, verifyKeys, data)

console.log(result)




/*


[{"name":"Class","key":"class","isRequired":true,"isOnly":false,"isSpecial":true,"c":"14"},{"name":"Type","key":"type","isRequired":true,"isOnly":false,"isSpecial":true,"c":"15"},{"name":"Model","key":"model","isRequired":false,"isOnly":false,"isSpecial":true,"c":"16"},{"name":"Config","key":"config","isRequired":false,"isOnly":false,"isSpecial":true,"c":"17"}] 和 [{"r":"1","c":"0","v":"20231208192625-0"},{"r":"1","c":"1","v":"test11001"},{"r":"1","c":"2","v":"inventoryTest001"},{"r":"1","c":"3","v":"0"},{"r":"1","c":"4","v":"在库"},{"r":"1","c":"5","v":"使用中"},{"r":"1","c":"6","v":""},{"r":"1","c":"7","v":""},{"r":"1","c":"8","v":""},{"r":"1","c":"9","v":"0"},{"r":"1","c":"10","v":"0"},{"r":"1","c":"11","v":""},{"r":"1","c":"12","v":""},{"r":"1","c":"13","v":""},{"r":"1","c":"14","v":"移动设备1"},{"r":"1","c":"15","v":"Apple"},{"r":"1","c":"16","v":"iPhone 14"},{"r":"1","c":"17","v":""},{"r":"1","c":"18","v":""},{"r":"1","c":"19","v":""},{"r":"1","c":"20","v":""},{"r":"1","c":"21","v":""},{"r":"1","c":"22","v":""},{"r":"1","c":"23","v":""},{"r":"1","c":"24","v":"FRCN-HQ"},{"r":"1","c":"25","v":""},{"r":"1","c":"26","v":""},{"r":"1","c":"27","v":""},{"r":"1","c":"28","v":""},{"r":"1","c":"29","v":""},{"r":"1","c":"30","v":""},{"r":"1","c":"31","v":""},{"r":"1","c":"32","v":""},{"r":"1","c":"33","v":""},{"r":"1","c":"34","v":""},{"r":"1","c":"35","v":""},{"r":"1","c":"36","v":""},{"r":"1","c":"37","v":""},{"r":"1","c":"38","v":""},{"r":"1","c":"39","v":""},{"r":"1","c":"40","v":""},{"r":"1","c":"41","v":""},{"r":"1","c":"42","v":""}] 为两个数组,现要求检测两者内部对象中,c相同,但是之间的


*/
console 命令行工具 X clear

                    
>
console