3

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, I.E. 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

1

5 Answers 5

6

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);

Sign up to request clarification or add additional context in comments.

1 Comment

thanks will try it out and come back , TY for the really quick answer and demo :)
3

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);

Comments

3

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);

Comments

1

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];
});

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.