javascript - How to convert uppercase format to camel case format
为情所困
为情所困 2017-05-19 10:46:43
0
2
559

For example: [{CARID:'111',CARNO:'1111'},{CARID:'222',CARNO:'2222'},{CARID:'333',CARNO:'3333'}]
Is there any general method to convert it into: [{carId:'111',carNo:'1111'},{carId:'222',carNo:'2222'},{carId:'333',carNo:' 3333'}]

为情所困
为情所困

reply all(2)
習慣沉默

If this topic is limited to the DEMO you provided and interpreted with a general method:

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"}]

It will be difficult to exceed it any further.

仅有的幸福

It’s actually very troublesome. You first need to have a dictionary with all the words in it, and then separate the key names into individual words according to the dictionary and then convert them into camel case format and then combine them.

You can imagine how troublesome this is.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!