changing object values to matching mapped values

Viewed 37

I have an Object that looks like this

const companyObj = 
    { 
      Advice_Charge: "B. In service / product cost"
      Org_Advisory: "B. 1-3"
      Org_Developers: "A. None"
      Org_MainCategory: "Business services and sales"
      Org_Name: "A1 Orchard Spreading"
      Org_Size_Facing: "B. 1-3"
      Org_SubCategory: "Contractor - Harvesting and cultivation"
    }

I have another object that looks like this

const sizeMap = {
          'A. None': '11-30',
          'B. 1-3': '1-3',
          'C. 4-10': '4-10',
          'D. 11 - 30': '11-30',
          'E. 31 - 100': '31-100',
          'F. 100 - 300': '100-300',
          'G. 301 - 1000': '301-1000',
          'H. 1,000+': '1000 +',
          'J. ???': '1-3',
        }

I am trying to change one of the properties inside the object using the sizeMap

I want companyObj to look like this

{Org_Size_Facing: '1-3'} // I don't really care about the other properties at this point..

Basically if companyObj.Org_Size_Facing returns any of the fields from sizeMap sizeMap should then return the appropriate value

I have tried

console.log(
          'objAsign',
          Object.assign(companyObj.Org_Size_Facing, sizeMap['Org_Size_Facing']),
        )

This is way off, but I'm sure you guys get the idea of it

Would appreciate any help you guys can offer

4 Answers

You can loop through the entries (pairs of key, value) of companyObj and use the in operator to check if the property is in the sizeMap object (note that it looks also in the prototype chain).

If you only need at least one property, then you can break early.

Running example

const companyObj = {
  Advice_Charge: "B. In service / product cost",
  Org_Advisory: "B. 1-3",
  Org_Developers: "A. None",
  Org_MainCategory: "Business services and sales",
  Org_Name: "A1 Orchard Spreading",
  Org_Size_Facing: "B. 1-3",
  Org_SubCategory: "Contractor - Harvesting and cultivation",
}


const sizeMap = {
  'A. None': '11-30',
  'B. 1-3': '1-3',
  'C. 4-10': '4-10',
  'D. 11 - 30': '11-30',
  'E. 31 - 100': '31-100',
  'F. 100 - 300': '100-300',
  'G. 301 - 1000': '301-1000',
  'H. 1,000+': '1000 +',
  'J. ???': '1-3',
}

for (const [key, val] of Object.entries(companyObj)) {
  if (val in sizeMap) {
    companyObj[key] = sizeMap[val];
    break; // if we dont need to check other properties
  }
}

console.log(companyObj)

Note that we are modifying the original object, we could also create a new one if thats a requirement.

You can do it with a for.. in loop

const companyObj = { 
      Advice_Charge: "B. In service / product cost",
      Org_Advisory: "B. 1-3",
      Org_Developers: "A. None",
      Org_MainCategory: "Business services and sales",
      Org_Name: "A1 Orchard Spreading",
      Org_Size_Facing: "B. 1-3",
      Org_SubCategory: "Contractor - Harvesting and cultivation"
}
const sizeMap = {
       'A. None': '11-30',
       'B. 1-3': '1-3',
       'C. 4-10': '4-10',
       'D. 11 - 30': '11-30',
       'E. 31 - 100': '31-100',
       'F. 100 - 300': '100-300',
       'G. 301 - 1000': '301-1000',
       'H. 1,000+': '1000 +',
       'J. ???': '1-3',
}

for(let key in companyObj) {
   if(companyObj[key] in sizeMap) {
      let val = companyObj[key];
      companyObj[key] = sizeMap[val];
   }
}

console.log(companyObj);

If you want to overwrite your original object you can do this:

if(sizeMap.hasOwnProperty(companyObj.Org_Size_Facing)){
    companyObj.Org_Size_Facing = sizeMap[companyObj.Org_Size_Facing]
}

Otherwise you can create a new object:

let newObj = {};

if(sizeMap.hasOwnProperty(companyObj.Org_Size_Facing)){
    newObj.Org_Size_Facing = sizeMap[companyObj.Org_Size_Facing]
}

console.log(newObj);

var newMap = sizeMap[companyObj.Org_Size_Facing];
if (newMap != undefined)
    companyObj.Org_Size_Facing = newMap;
Related