When the key is handling digital

Colleagues across the interface returns the value of the key, the key you want to want to deal with the subject, then write a method to handle similar data

'{ "1": "With admission certificate (admission auxiliary code or two-dimensional code) admission order details page", "2": "With the next single admission reservation ID", "3" : ""} 'key you want to handle objects
    const content = '{ "1" : " With admission certificate (admission auxiliary code or two-dimensional code) admission order details page", "2": "With the next single admission reservation ID" ". 3": ""} ' 
    const newKey = [' key1 ',' key2 ',' key3 ',' KEY4 ' ] 

    function keyConversion (Content =' {} ', newKey = []) { 
        const obj = the JSON.parse (Content)
         IF (Object.keys (obj) .length == 0) return {} 
        the let [ARR, index] = [ 
            [], -1 
        ] 
        for (the let Key in obj) { // iterate the pick object value 
            arr.Push (obj [key]) // can come value corresponding to the digital key by obj [key] of the key when a digital manner 
        }
        return arr.reduce ((pre, CUR) => { 
            index ++
             return Object.assign (pre, { // increments incorporated within the object to a new key 
                [newKey [index]]: CUR 
            }) 
        } , {}) 
    } 
    const newobj = keyConversion (Content, newKey) 
    the console.log (newobj)

 

 

Guess you like

Origin www.cnblogs.com/dajuyiding/p/11515645.html