Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

大写格式怎么转换为驼峰格式

比如说:[{CARID:'111',CARNO:'1111'},{CARID:'222',CARNO:'2222'},{CARID:'333',CARNO:'3333'}]
有没有什么通用的方法转化为:[{carId:'111',carNo:'1111'},{carId:'222',carNo:'2222'},{carId:'333',carNo:'3333'}]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果把这个话题局限在你提供的DEMO中的话,并且以通用方法来解读的话:

Array.prototype.toCamelCase = function(keys) {
    keys  = keys || { CARID: 'carId', CARNO: 'carNo' };
    return this.map(item => {
        let newItem = {};
        for (let ii in item) {
            newItem[keys[ii] || ii] = item[ii];
        }
        return newItem;
    });
}

console.log(JSON.stringify(data.toCamelCase()));
// [{"carId":"111","carNo":"1111"},{"carId":"222","carNo":"2222"},{"carId":"333","carNo":"3333"}]
console.log(JSON.stringify(data.toCamelCase({ CARID: 'carID', CARNO: 'carNO' })));
// [{"carID":"111","carNO":"1111"},{"carID":"222","carNO":"2222"},{"carID":"333","carNO":"3333"}]

再超就难了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...