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"}} }
    */
  • 相关阅读:
    【Mysql sql inject】【入门篇】sqli-labs使用 part 3【15-17】
    【Mysql sql inject】【入门篇】SQLi-Labs使用 part 2【12-14】
    【Mysql sql inject】【入门篇】SQLi-Labs使用 part 1【01-11】
    【CTF WEB】ISCC 2016 web 2题记录
    【Mysql sql inject】POST方法BASE64编码注入write-up
    【sql server inject】使用动态查询执行sql语句实例
    【跨站关】网络信息安全攻防学习平台跨站过关的彩蛋
    【sql inject】sql盲注技巧
    【php】随缘php企业网站管理系统V2.0 shownews.php注入漏洞
    ASP.NET新建解决方案和网站
  • 原文地址:https://www.cnblogs.com/time-on/p/7852010.html
Copyright © 2011-2022 走看看