如何解决对象密钥更改的问题?

时间:2016-03-30 11:33:55

标签: javascript jquery

我有一个标准对象,我需要将密钥更改为小写。它正在改变,但类型格式也在发生变化。在对象rowSet中是一个数组。转换后也需要是数组。但它正在改变为对象。请看看你会得到我的观点的对象。

实际对象:

var obj = {
            "Collections": {
                "conTainer": {
                    "rowSet": [{
                        "containerIsArchived": "Null",
                        "containerOrderNo": "26",
                        "versionNum": "0",
                        "containerGlobalUniqueId": "Null",
                        "containerIsTenantBased": "true",
                        "containerCreatedBy": "user",
                        "containerIsDeleted": "false",
                        "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
                        "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
                        "containerApplicationId": "0000000-0000-0000-0000-000000000000",
                        "containerIsActive": "Null",
                        "containerHeaderText": "apitest19feb16",
                        "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
                        "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
                    }]
                }
            }
        };

密钥转换为小写后:

{
  "collections": {
    "container": {
      "rowset": {
        "0": {
          "containerisarchived": "Null",
          "containerorderno": "26",
          "versionnum": "0",
          "containerglobaluniqueid": "Null",
          "containeristenantbased": "true",
          "containercreatedby": "user",
          "containerisdeleted": "false",
          "containertenantid": "292FEC76-5F1C-486F-85A5-09D88096F098",
          "containerlayoutid": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
          "containerapplicationid": "0000000-0000-0000-0000-000000000000",
          "containerisactive": "Null",
          "containerheadertext": "apitest19feb16",
          "containerid": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
          "containercreatedutcdate": "2016-02-19 17:57:51.0"
        }
      }
    }
  }
}

在这里,您可以比较实际对象和转换对象的rowSet

以下是小提琴链接:http://jsfiddle.net/bagya1985/r8mbz49h/

3 个答案:

答案 0 :(得分:1)

您需要首先检查您是否正在处理属于数组的属性,如果是,请在output中创建一个新数组。然后,您可以遍历数组中的所有值,递归调用您的方法,以便随时使用小写字母。 Here是一个工作小提琴(编辑以处理数组中的多个项目)



var obj = {
    "Collections": {
        "conTainer": {
            "rowSet": [{
                "containerIsArchived": "Null",
                "containerOrderNo": "26",
                "versionNum": "0",
                "containerGlobalUniqueId": "Null",
                "containerIsTenantBased": "true",
                "containerCreatedBy": "user",
                "containerIsDeleted": "false",
                "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
                "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
                "containerApplicationId": "0000000-0000-0000-0000-000000000000",
                "containerIsActive": "Null",
                "containerHeaderText": "apitest19feb16",
                "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
                "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
            },{
                "containerIsArchived": "Null",
                "containerOrderNo": "26",
                "versionNum": "0",
                "containerGlobalUniqueId": "Null",
                "containerIsTenantBased": "true",
                "containerCreatedBy": "user",
                "containerIsDeleted": "false",
                "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
                "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
                "containerApplicationId": "0000000-0000-0000-0000-000000000000",
                "containerIsActive": "Null",
                "containerHeaderText": "apitest19feb16",
                "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
                "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
            }
            ]
        }
    }
};

console.log(JSON.stringify(ConvertKeysToLowerCase(obj)));


function ConvertKeysToLowerCase(obj) {
    var output = {};
    for (i in obj) {
        if(Object.prototype.toString.apply(obj[i]) === '[object Array]'){
        		var key = i.toLowerCase();
        		output[key] = [];
            var arr = obj[i];
            arr.forEach(function(o){  
            	output[key].push(ConvertKeysToLowerCase(o));
            });
        } else if (typeof obj[i] == 'object') { 
            output[i.toLowerCase()] = ConvertKeysToLowerCase(obj[i]);
        } else {  
            output[i.toLowerCase()] = obj[i];
        }
    }
    return output;
};




答案 1 :(得分:1)

此提议使用函数迭代数组或对象的键。

function lowerKeys(object) {            
    if (Array.isArray(object)) {
        object.forEach(lowerKeys);
        return;
    }
    if (typeof object === 'object') {
        Object.keys(object).forEach(function (k) {
            lowerKeys(object[k]);
            object[k.toLowerCase()] = object[k];
            delete object[k];
        });
    }
}

var obj = { "Collections": { "conTainer": { "rowSet": [{ "containerIsArchived": "Null", "containerOrderNo": "26", "versionNum": "0", "containerGlobalUniqueId": "Null", "containerIsTenantBased": "true", "containerCreatedBy": "user", "containerIsDeleted": "false", "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098", "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf", "containerApplicationId": "0000000-0000-0000-0000-000000000000", "containerIsActive": "Null", "containerHeaderText": "apitest19feb16", "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6", "containerCreatedUTCDate": "2016-02-19 17:57:51.0" }] } } };

lowerKeys(obj);
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');

答案 2 :(得分:1)

以下是适合您案例的完整答案:

var obj = {
            "Collections": {
                "conTainer": {
                    "rowSet": [{
                        "containerIsArchived": "Null",
                        "containerOrderNo": "26",
                        "versionNum": "0",
                        "containerGlobalUniqueId": "Null",
                        "containerIsTenantBased": "true",
                        "containerCreatedBy": "user",
                        "containerIsDeleted": "false",
                        "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
                        "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
                        "containerApplicationId": "0000000-0000-0000-0000-000000000000",
                        "containerIsActive": "Null",
                        "containerHeaderText": "apitest19feb16",
                        "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
                        "containerCreatedUTCDate": "2016-02-19 17:57:51.0"
                    }]
                }
            }
        };

matches = [];

// 1) convert the object to a string 
tmpObjectStringified = JSON.stringify(obj);

// 2) search for javascript object keys ( of the format "key": )
regex = /\"(\w+?)\":/g; // 

match  = regex.exec(tmpObjectStringified);
while(match){     
    matches.push(match[1]);     
    match = regex.exec(tmpObjectStringified);
}

// 3) now all matches are found in `matches` array.
matches.map(function(key){
    tmpObjectStringified =   tmpObjectStringified.replace('"'+key+'":','"'+key.toLowerCase()+'":');
});

// console.log(matches);
console.log(tmpObjectStringified);

obj = JSON.parse(tmpObjectStringified);

console.log(obj);

希望这有帮助。