Reputation: 623
Is there anyway to take an object like:
{
"computer_general_last_contact_time_epoch": 1566522907515,
"computer_general_last_contact_time_utc": "2019-08-23T01:15:07.515+0000",
"computer_general_initial_entry_date": "2018-08-18",
}
And make each property name shorter so that each word better _
characters of object keys are shortened to a fixed character length. So for instance, shortening to a fixed length of 3 characters would produce:
{
"com_gen_las_con_tim_epo": 1566522907515,
"com_gen_las_con_tim_utc": "2019-08-23T01:15:07.515+0000",
"com_gen_ini_ent_dat": "2018-08-18",
}
Would I have to create a new object? Iterate through each object in my current object, use regex to remove all '_' and then shorten each word then add them all together with _ inbetween? I'm super lost on this as I have objects with 100's of properties that I need to shorten for an integration and any advice would help!
Upvotes: 1
Views: 494
Reputation: 65806
If you simply want to shorten the existing property names, you'd create a new objecct, but you can create a second set of property names on the existing object (by iterating over the keys) that simply map back to the longer named ones.
let obj = {
"computer_general_last_contact_time_epoch": 1566522907515,
"computer_general_last_contact_time_utc": "2019-08-23T01:15:07.515+0000",
"computer_general_initial_entry_date": "2018-08-18",
};
// Loop over the object
for(let p in obj){
// Create a new string as you see fit
let newProp = p.replace("computer_", "com_").replace("_general_", "_gen_").replace("_initial_", "_ini_").replace("_entry_","_ent_").replace("_date","_dat");
// Add the new key that points to the existing value
obj[newProp] = obj[p];
}
console.log(obj); // Updated object
// And then you can access the property with the new key
console.log(obj.com_gen_ini_ent_dat);
Upvotes: 1
Reputation: 13389
You can do something like this:
var obj = {
"computer_general_last_contact_time_epoch": 1566522907515,
"computer_general_last_contact_time_utc": "2019-08-23T01:15:07.515+0000",
"computer_general_initial_entry_date": "2018-08-18",
}
// Note: this version works only for snake case .
function shortandObjectKeys(obj) {
var newObj = {}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var newKey = key.split('_').map(k => k.substr(0, 3)).join('_');
newObj[newKey] = obj[key];
}
}
return newObj;
}
console.log(shortandObjectKeys(obj));
Upvotes: 1
Reputation: 8040
Your solution should consist of two stages:
function shortify(str) {
return str.split('_').map(word => word.substr(0, 3)).join('_')
}
const newObject = {};
for (let key in originalObject) {
newObject[shortify(key)] = originalObject[key];
}
The whole solution could look as follows:
const initialObject = {
"computer_general_last_contact_time_epoch": 1566522907515,
"computer_general_last_contact_time_utc": "2019-08-23T01:15:07.515+0000",
"computer_general_initial_entry_date": "2018-08-18",
};
function shortify(str) {
return str.split('_').map(word => word.substr(0, 3)).join('_')
}
const newObject = {};
for (let key in initialObject) {
newObject[shortify(key)] = initialObject[key];
}
console.log(newObject);
Upvotes: 1
Reputation: 30360
A simple solution would be to iterate each key of your input object, and perform the following per key to compute the new key to transfer values to:
In code this can be expressed as:
let obj = {
"computer_general_last_contact_time_epoch": 1566522907515,
"computer_general_last_contact_time_utc": "2019-08-23T01:15:07.515+0000",
"computer_general_initial_entry_date": "2018-08-18",
};
const newObj = {};
/* Iterate keys in obj */
for(let key in obj){
/* Create new key, split key by _ to extract words, trim each word to three
letters, join trimmed words by _ again to obtain new key */
const newKey = key.split("_").map(word => word.substr(0, 3)).join("_");
/* Apply key value from obj to new obj with new key */
newObj[newKey] = obj[key];
}
console.log(newObj);
Upvotes: 1