• rename-修改对象属性名称(转)


    function renameProperties(sourceObj, replaceList, destObj) {
        destObj = destObj || {};
        // for each property in source object
        $.each(sourceObj, function(key) {
            // if the property really exist
            if(sourceObj.hasOwnProperty(key)) {
    
                // if the child key is array
                if(sourceObj[key] instanceof Array) {
                    // if it in the replace List (as property)
                    if(replaceList[key]) {
                        var newName = replaceList[key];
                        destObj[newName] = [];
                        // send it to replaceAttrNames() function (recursively)
                        renameProperties(sourceObj[key], replaceList, destObj[newName]);
    
                        // if its NOT in the replace List (as property)
                    } else if(!replaceList[key]) {
                        destObj[key] = [];
                        renameProperties(sourceObj[key], replaceList, destObj[key]);
                    }
    
                    // if the child key is object
                } else if(typeof sourceObj[key] === 'object') {
                    // if it in the replace List (as property)
                    if(replaceList[key]) {
                        var newName = replaceList[key];
                        // create new property in the destObj named as the new name
                        destObj[newName] = {};
                        // send it to replaceAttrNames() function (recursively)
                        renameProperties(sourceObj[key], replaceList, destObj[newName]);
    
                        // if its NOT in the replace List (as property)
                    } else if(!replaceList[key]) {
                        destObj[key] = {};
                        renameProperties(sourceObj[key], replaceList, destObj[key]);
                    }
    
                    // if the child key is NOT object and NOT Array
                } else {
                    // if it in the replace List (as property)
                    if(replaceList[key]) {
                        var newName = replaceList[key];
                        destObj[newName] = sourceObj[key];
                        // if its NOT in the replace List (as property)
                    } else if(!replaceList[key]) {
                        destObj[key] = sourceObj[key];
                    }
                }
    
            }
        });
    
        return destObj;
    }
    
    
    // NOTE: If you are using Jquery OR underscore.js Or another library that has 'each()' function, you can use it Instead This function,
    // (You will need to replace the call to 'each()' in 'renameProperties()' to your 'each()', in Jquery: '$.each()', and in underscore.js: '_.each()').
    // function each(objOrArr, callBack) {
    //     // if we got Array
    //     if(objOrArr instanceof Array) {
    //         for(var i = 0; i < objOrArr.length; i++) {
    //             callBack(i);
    //         }
    //
    //         // if we got an Object
    //     } else if(typeof objOrArr === 'object') {
    //         for(var prop in objOrArr) {
    //             // if the property really exist
    //             if(objOrArr.hasOwnProperty(prop)) {
    //                 callBack(prop);
    //             }
    //         }
    //     }
    //
    // }
    
    
    /*
    === usage: ===
    var obj = {foo: 'word a', bar: {some: 'word b', thing: 'word c'}};
    var replacedObj = renameProperties(obj, {foo: 'baz', bar: 'qux'});
    // the output will be (as JSON): {"obj": {"baz": "word a", "qux": {"some": "word b", "thing": "word c"}} }
    */
  • 相关阅读:
    关闭Windows的CTRL+ALT+DEL 和 ALT+TAB组合键的方法
    VC中常用文件操作(一)
    VC 创建NT服务程序
    几个操作文件的API函数
    VC中常用文件操作(二) ——INI文件操作
    初学OpenCV之图像坐标读取函数cvGet2D()
    OpenCV学习之图像读取与显示
    SLT容器之vector简单实现C++运行时多态
    C/C++文件——数据写入、读取
    OpenCV学习之视频读取与帧的提取、显示及保存
  • 原文地址:https://www.cnblogs.com/time-on/p/7852010.html
Copyright © 2020-2023  润新知