JavaScript: Object Rename Key

Rename Key in JS Object with values from another array

You could do it with a for loop as follows.

let title = [
'Total Cryptocurrencies',
'Total Markets',
'Total Exchanges',
'Total Market Cap',
'Total 24h Volume',
];

var obj = {
1: 5,
2: 7,
3: 0,
4: 0,
5: 0,
};

var newObj = {};

for(i = 0; i < title.length; i++) {
newObj[title[i]] = obj[(i+1).toString()];
}

var obj = newObj;

How to change a Key in Object if Key value is number?

To change a key if it's value is a number, you need to clone the key and value first.
And then you need to delete the key in the object and once it's deleted it should be injected into the object again.

`function change(obj) {
for (var prop in obj) {
if (typeof obj[prop] === "number") {
obj['is_number_' + obj[prop]] = obj[prop]
delete obj[prop]
}
}
}`

The result should be like this :
is_number_100 : 100,
is_number_200 : 200,
is_number_300 : 300,
whisky : "not in stock"

How to rename key of object?

You can assign the value to a new key and delete the old one.

let o = {
keyName_1: {
prop: 'prop1'
}
};
for (const key of Object.keys(o)) {
if (key.includes('_1')) {
o[key.replace('_1','')] = o[key];
delete o[key];
}
}
console.log(o);

Javascript how to switch object keys or rename object key?

There is two parts to this story:

Given

const order = ["melon", "pumpkin", "beard", "lizard"];

const object1 = {
"?": "!",
"pumpkin": "spice",
"beard": "shaved",
"lizard": "green",
"melon": "seed"
};

Is there a specific order and unknown properties should be omitted?

console.log(order.map(key => object1[key]))

Do you want to order all properties using a certain sorting function:

const sorted = Object
.keys(object1)
.sort(function (a, b) {
/* or any sorting algorithm (alphabetical, numerical, ...) */
const indexA = order.indexOf(a)
const indexB = order.indexOf(b)

return indexA - indexB;

});
console.log(sorted.map(key => object1[key]))

jsbin

Change object key using Object.keys ES6

Here's how I solved it. I used a map to map between existing key and new key. Just substitute the map with whatever new values you need. Finally remove old keys from the object using omit.

var tab = {
abc:1,
def:40,
xyz: 50
}

var map = {
abc : "newabc",
def : "newdef",
xyz : "newxyz"
}

_.each(tab, function(value, key) {
key = map[key] || key;
tab[key] = value;
});

console.log(_.omit(tab, Object.keys(map)));

Rename Keys in Object containing child objects

Maybe like this:

var objs = {

"one":{

"title":"bla",

"amount":5,

"children":[

{

"title":"bla",

"identifier":"some text"

},

{

"title":"bla2",

"identifier":"some text2"

}

]

},

"two":{

"title":"bla",

"amount":5,

"children":[

{

"title":"bla",

"identifier":"some text"

},

{

"title":"bla2",

"identifier":"some text2"

}

]

}

};

function transorm_obj(_obj){

var out = [];

for(var key in _obj){

var new_obj = {};

for(var prop in _obj[key]){

if(prop == 'children'){

new_obj['items'] = transorm_obj(_obj[key][prop]);

}else if(prop == 'title'){

new_obj['text'] = _obj[key][prop];

}else{

new_obj[prop] = _obj[key][prop];

}

}

out.push(new_obj);

}

return out;

}

console.log(transorm_obj(objs));


Related Topics



Leave a reply



Submit