dbrree
dbrree

Reputation: 623

JavaScript: Shorten each word in properties of object to fixed length?

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

Answers (4)

Scott Marcus
Scott Marcus

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

V. Sambor
V. Sambor

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

Sergey Mell
Sergey Mell

Reputation: 8040

Your solution should consist of two stages:

  1. Create a property "shotification" function i.e.
function shortify(str) {
    return str.split('_').map(word => word.substr(0, 3)).join('_')
}
  1. Iterate over initial object properties and generate a new one:
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

Dacre Denny
Dacre Denny

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:

  1. Split key by "_" to get words from key
  2. Trim each word to 3 letters
  3. Join trimmed words with "_" to obtain new key

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

Related Questions