比较数组-比较两个数组是 删除/新增/更新

2018-04-04  本文已影响0人  Romyo
先搞清要做什么?
1.比较两个新旧数组,判断是哪种情况------------- 删除/新增/更新 
2.查询字典,利用引用类型的特性返回出对应的对象
测试这个函数需要准备的参数
1.mockoriginConfigValue---旧数据
2.mockconfigValue    -----新数据
3.dictionary         -----字典,用来转换成中文
4.returnObj          ----利用引用类型的特性,返回的obj
即将要测试的函数
findDifferentData(mockoriginConfigValue, mockconfigValue, dictionary, obj)
参数1,参数2
情况1---删除
oldData=[A,B,C]
newData=[A,B]

const oldItem=null; //存放未被删之前的数据 
const newItem=null; //存放找到的被删除后的这条undefined数据

if(oldData.length>newData.length){  '删除'
       for(let i=0;i<oldData.length;i++){
                if(!newData[i]){//说明C不存在于newData里面
                    oldItem=oldData[i]  ==>C
                    newItem=newData[i]  ==>被删后的状态
                }
        }
}

测试用的mock数据用这个测上面的findDifferentData函数
   const mockoriginConfigValue = JSON.parse(`[["457","458"],["457"]]`); 相当于[A,B]
   const mockconfigValue = JSON.parse(`[["457","458"]]`);               相当于[A]
情况2---新增
oldData=[A,B]
newData=[A,B,C]

const oldItem='空'
const newItem=newData[newData.length-1] //最后一条就是新增出来的  即 C


测试用的mock数据用这个测上面的findDifferentData函数
   const mockoriginConfigValue = JSON.parse(`[["457","458"]]`); 相当于[A]
   const mockconfigValue = JSON.parse(`[["457","458"],["457"]]`); 相当于[A,B]
情况3--更新
oldData=[A,B,C]
newData=[A,B,D]

const oldItem=null; //存放更新前的数据 
const newItem=null; //存放更新后的数据

if(oldData.length==newData.length){
      for(let i=0;i<newData.length;i++) {
            if(newData[i] !== oldData[i] ){  //这边要视情况而定 如果C!==D
                oldItem=oldData[i]
                newItem= newData[i]   
            }
      } 
} 
测试用的mock数据用这个测上面的findDifferentData函数
const mockoriginConfigValue = JSON.parse(`[["457","458"],["457"]]`); 相当于[A,B]
const mockconfigValue = JSON.parse(`[["457","458"],["457","333"]]`); 相当于[A,C]
参数3
const dictionary = {
    "332": "332-区块2",
    "333": "333-区块1",
    "349": "349-区块3",
    "429": "429-区块1",
    "430": "430-区块2",
    "431": "431-区域1",
    "442": "442-区块1",
    "443": "443-区块2",
    "457": "457-区块2",
    "458": "458-区块6",
    "459": "459-区块4",
    "460": "460-区块3",
    "461": "461-区块5",
    "462": "462-野生动物园",
    "463": "463-测试新增区块",
    "464": "464-区块8 新",
    "465": "465-区块9 新",
    "466": "466-区块10",
    "467": "467-区块1",
    "468": "468-湘湖及杭州乐园",
    "482": "482-区块12",
    "483": "483-区块13",
    "495": "495-区块1",
    "496": "496-区块2",
    "497": "497-区块3",
    "499": "499-区块2",
    "501": "501-区块4",
    "503": "503-区块4",
    "504": "504-区块1",
    "505": "505-区块2",
    "506": "506-区块1",
    "508": "508-阿里巴巴西溪园区",
    "509": "509-临时",
    "519": "519-区块1",
    "520": "520-区块1",
    "521": "521-区块3",
    "526": "526-区块2",
    "551": "551-name3",
    "558": "558-天安门",
    "559": "559-天坛公园",
    "560": "560-朝阳公园",
    "561": "561-石景山",
    "562": "562-龙德广场",
    "563": "563-朝阳公园",
    "564": "564-东风公园",
    "569": "569-乌鲁木齐市植物园",
    "570": "570-头屯河公园",
    "571": "571-乌鲁木齐市政府",
    "572": "572-乌鲁木齐北郊客运站",
    "574": "574-北京人工体育场",
    "575": "575-成都动物园",
    "576": "576-深圳东站",
    "577": "577-贵阳站",
    "579": "579-青岛站",
    "583": "583-青岛世博园景区",
    "606": "606-台北兴华公园",
    "607": "607-水稚公园",
    "608": "608-水车博览园",
    "609": "609-兰州水车园",
    "610": "610-兰州汽车南站",
    "611": "611-兰州西站",
    "612": "612-龙头山森林公园",
    "640": "640-111",
    "641": "641-222",
    "642": "642-333",
    "644": "644-444",
    "645": "645-萧山机场",
    "647": "647-宋城",
    "650": "650-广化寺",
    "652": "652-666",
    "656": "656-n4",
    "691": "691-123",
    "692": "692-234",
    "693": "693-123",
    "694": "694-123",
    "695": "695-234",
    "696": "696-1",
    "697": "697-2",
    "698": "698-西庐区",
    "699": "699-主区域",
    "700": "700-主配送区块",
    "701": "701-主配送区",
    "711": "711-dddddd",
    "712": "712-123",
    "713": "713-太炎小学",
    "715": "715-99999",
    "720": "720-分区名称",
    "721": "721-n1",
    "722": "722-盒饭",
    "723": "723-n1",
    "724": "724-n2",
    "725": "725-n3",
    "726": "726-dcdcdc1222",
    "727": "727-11111",
    "730": "730-1204",
    "732": "732-XYJ",
    "734": "734-XYJ",
    "735": "735-午潮山",
    "736": "736-西溪园区",
    "737": "737-世树测试",
    "738": "738-世树测试2",
    "739": "739-测试3",
    "740": "740-区块14",
    "741": "741-区块15",
    "742": "742-区块16",
    "743": "743-同济大学",
    "744": "744-上海理工大学",
    "745": "745-杭州南站",
    "746": "746-邵逸夫医院",
    "747": "747-附属儿童医院",
    "748": "748-图书馆",
    "749": "749-大罗山",
    "750": "750-阿里巴巴",
    "751": "751-武林广场"
}
参数4
const obj = {}
运行此函数
function findDifferentData(oldArr, newArr, dictionary, obj) {
    let newItem = [];
    let oldItem = [];
    // 删除情况
    if (oldArr.length > newArr.length) {
        for (let i = 0; i < oldArr.length; i++) {
            if (!newArr[i]) {
                newItem = newArr[i];
                oldItem = oldArr[i];
            }
        }
        Object.assign(obj, {
            status: '删除',
            beforeData: renderAccordingDictionary(dictionary, oldItem),
            afterData: !!renderAccordingDictionary(dictionary, newItem) && !!renderAccordingDictionary(
                dictionary,
                newItem
            ) || '已删'
        });
        console.log(obj)
    } else if (oldArr.length < newArr.length) { // 新增情况
        Object.assign(obj, {
            status: '新增',
            beforeData: '之前无该数据',
            afterData: renderAccordingDictionary(dictionary, newArr[newArr.length - 1])
        })
        console.log(obj)
    } else {
        for (let i = 0; i < newArr.length; i++) {
            if (newArr[i].length !== oldArr[i].length) {
                newItem = newArr[i];
                oldItem = oldArr[i];
            }
        }
        Object.assign(obj, {
            status: '更新',
            beforeData: renderAccordingDictionary(dictionary, oldItem),
            afterData: renderAccordingDictionary(dictionary, newItem)
        })
        console.log(obj)
    }
}
上一篇 下一篇

猜你喜欢

热点阅读