• 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"}} }
    */
  • 相关阅读:
    测试用例
    SQL/My sql
    测试报告
    postman
    Linux环境搭建及命令
    jQuery EasyUI API 中文文档 树表格(TreeGrid)
    MYSQL学习心得(十) 自定义存储过程和函数
    webBrowser
    ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)前言与目录
    vim括号操作(转)
  • 原文地址:https://www.cnblogs.com/time-on/p/7852010.html
Copyright © 2020-2023  润新知