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
What Does the Exclamation Mark Do Before the Function
Why Can't I Directly Modify a Component'S State, Really
Scroll to an Element With Jquery
Short Circuit Array.Foreach Like Calling Break
How to Get the Value of Text Input Field Using JavaScript
How to Get Client'S Ip Address Using JavaScript
How to Debug My JavaScript Code
Check Variable Equality Against a List of Values
Setting "Checked" For a Checkbox With Jquery
How to Encode a String to Base64 in JavaScript
How to Round to At Most 2 Decimal Places, If Necessary
How to Create and Read a Value from Cookie With JavaScript
Why Is Extending Native Objects a Bad Practice
Convert Date to Another Timezone in JavaScript
Window.Onload VS $(Document).Ready()
How to Attach Events to Dynamic HTML Elements With Jquery