Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get object path for all property in js

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'
    }
]

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Jason path finding and parsing should work dynamically

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading