js modified object key value

    each array = [
        {
            id:1,
            name: "Bob"
        },
        {
            id:2,
            name: "Little Red"
        }
    ];
    
    / ** / 
    // old key to a new key mapping 
    var KEYMAP = {
         "ID": "value" ,
         "name": "label"
    };
    
    for(var i = 0;i < array.length;i++){
            var obj = array[i];
            for(var key in obj){
                       var newKey = keyMap[key];
                       if(newKey){
                                obj[newKey] = obj[key];
                                delete obj[key];
                         }
                }
    }
    
    console.log(array);

The original address: https: //segmentfault.com/q/1010000015794387

Modify key value 

 

Guess you like

Origin www.cnblogs.com/IwishIcould/p/11780195.html