skip to Main Content

How to get the object path for all properties from the below object

  {
    employeeInfo: {
        address1: '2nd street',
        address2: 'eb colony',
        city: 'coimbatore'
    }
 }

Expected result

 {
fields: [
    {
        fieldId: 'employeeInfo.address1',
        value: '2nd street'
    },
    {
        fieldId: 'employeeInfo.city',
        value: 'coimbatore'
    },
    {
        fieldId: 'employeeInfo.country.countryname',
        value: 'india'
    },
    {
        fieldId: 'employeeInfo.country.isoalphacode2',
        value: 'IN'
    }
]

}

Jason path finding and parsing should work dynamically

2

Answers


  1. To dynamically find and parse the object path for all properties in the given object, you can use a recursive function. Here’s an example implementation in

    function getObjectPath(obj, prefix = '', result = []) {
      for (let key in obj) {
        if (typeof obj[key] === 'object') {
          getObjectPath(obj[key], prefix + key + '.', result);
        } else {
          result.push({
            fieldId: prefix + key,
            value: obj[key]
          });
        }
      }
      return result;
    }
    
    // Example usage
    const data = {
      employeeInfo: {
        address1: '2nd street',
        address2: 'eb colony',
        city: 'coimbatore',
        country: {
          countryname: 'india',
          isoalphacode2: 'IN'
        }
      }
    };
    
    const result = {
      fields: getObjectPath(data)
    };
    
    console.log(result);
    

    This code defines the getObjectPath function, which recursively traverses the object and builds the object path for each property. It takes three parameters: the object to traverse (obj), the current prefix (initialized as an empty string), and the result array to store the field objects.

    Within the function, it iterates over the keys of the object. If the value of a key is another object, it calls getObjectPath recursively with an updated prefix (including the current key). If the value is not an object, it adds a field object to the result array with the fieldId as the concatenation of the prefix and the current key, and the value as the value of the property.

    In the example usage, we provide the data object and assign the result of getObjectPath(data) to the fields property of the result object.

    The output will be displayed in the console, containing the expected result with the object path and corresponding values for each property in the data object.

    Login or Signup to reply.
  2. const obj =   {
        employeeInfo: {
            address1: '2nd street',
            address2: 'eb colony',
            city: 'coimbatore'
        }
     }
     
    console.log(getFields(obj))
    
    function getFields(obj) {
      const fields = []
      
      req(obj, [])
      
      return { fields }
      
      function req(o, str) {
        if (typeof o !== 'object') {
          fields.push({
            fieldId: str.join('.'),
            value: o
          })
          return
        }
        for (const key in o) {
          req(o[key], [...str, key])
        }
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search