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

本文介绍了一个实用的JavaScript函数,用于批量重命名对象的属性。通过递归处理,该函数能够深入对象的每个层级,将指定的属性名替换为新的名称。无论是处理简单的键值对,还是复杂的嵌套结构,此函数都能胜任。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"}} }
*/

 

转载于:https://www.cnblogs.com/time-on/p/7852010.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值