zoukankan      html  css  js  c++  java
  • 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"}} }
    */
  • 相关阅读:
    redis为何单线程 效率还这么高 为何使用跳表不使用B+树做索引(阿里)
    阿里笔试-生产者消费者模式
    调用 redis 原子命令,保证多线程安全 的incr命令问题
    忽略警告注解@SuppressWarnings详解
    怎样查出连续数字中缺失的数字
    IDEA-Maven的Dependencies中出现红色波浪线
    Java实现AES加密(window机器和linux机器) 注意window机器 和linux机器算法稍有不同
    PR代码提交规范
    接口的幂等性,如何保证
    bash 括号(小括号,双小括号,中括号,双中括号,大括号)
  • 原文地址:https://www.cnblogs.com/time-on/p/7852010.html
Copyright © 2011-2022 走看看