search

Home  >  Q&A  >  body text

javascript - How to convert uppercase format to camel case format

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

为情所困为情所困2854 days ago609

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-05-19 10:48:43

    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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:48:43

    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.

    reply
    0
  • Cancelreply