简体   繁体   中英

How to change object key values in javascript object

Say i have the following object:

obj1 = {
   'patato la blaza' : {
        'weight' : 5,
        'height' : 90
    },
    'apple ibiza' : {
        'weight' : 3,
        'height' : 84
    }
}

I would like to run through the keys and modify them, IE change the key values, how do i go about doing this in javascript/jQuery? , i know i can modify the value like so:

$.each(obj1 , function(i , e){
   obj1[i] = "someramdomvalue"
});

But i don't quite understand how would i go about change/manipulating the values of the keys, in the object provided above i would like to change the value of patato la blaza to just patato and apple ibiza to apple , i will use a regex to get the shortened version, but i don't know how to add these shortened values as the new key in the object. would appreciate any help.

NOTE::- i have searched SO already but most of the questions pertain to how to change the value of the value ob an object and not the keys

I'd use reduce on the Object.keys()

 let obj = { 'patato la blaza': { 'weight': 5, 'height': 90 }, 'apple ibiza': { 'weight': 3, 'height': 84 } }; obj = Object.keys(obj).reduce((a, b) => { a[b.substring(0, b.indexOf(' '))] = obj[b]; return a; }, {}); console.log(obj); 

Probably something along those lines:

 const obj1 = { 'patato la blaza' : { 'weight' : 5, 'height' : 90 }, 'apple ibiza' : { 'weight' : 3, 'height' : 84 } }; // Example key transform const transform = x => x.split(" ")[0]; Object.keys(obj1).forEach(key => { const val = obj1[key]; delete obj1[key]; obj1[transform(key)] = val; }); console.log(obj1); 

If you want to use $.each() , I would create a new property with the desired key, copy the value of the existing property and then delete the old property:

$.each(obj1 , function(i , e){
   obj1["somenewkey"] = obj1[i];
   delete obj1[i];
});

Create a map of all the changes you want and build a new object with the new keys names:

 var obj = { 'patato la blaza' : { 'weight' : 5, 'height' : 90 }, 'apple ibiza' : { 'weight' : 3, 'height' : 84 } }; var changes = { 'patato la blaza': 'potato', 'apple ibiza': 'apple' } var res = Object.assign({}, ...Object.entries(obj).map(([prevKey, val]) => ({[changes[prevKey]]: val}))); console.log(res); 

It looks like the assign function is best, but the answer above needs work to handle keys that aren't changed.

For quick and dirty change way to change one value's key:

obj[newId] = JSON.parse(JSON.stringify(obj[oldId]))
delete obj[oldId]

 var obj = { 'patato la blaza': { 'weight': 5, 'height': 90, 'arr': [1,2,3, {'a': 5}] }, 'apple ibiza': { 'weight': 3, 'height': 84, 'arr': [2,4,6, {'b': 15}] } }; obj['Potato'] = JSON.parse(JSON.stringify(obj['patato la blaza'])) delete obj['patato la blaza'] console.log(obj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM