Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
javascript for loop with recursion and return value example

I am trying to convert json object into array of path string but not getting expected result. Don't know might be other way to achieve the same but tried using for loop and lodash isObject and isEmpty for validate object like below.

Input Json:

    const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

javascript code:

var pathArray = this.convertJsonToArrayOfPathString(json);
    console.log(pathArray);
convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      path = path ? path + '.' + key : key;

      if (_.isObject(json[key]) && !_.isEmpty(json[key])) {
        this.convertJsonToArrayOfPathString(json[key], pathArray, path);
      }
      else {
        pathArray.push(path)
        path = ''
      }
    }
    return pathArray;
  }

expected output:

[
      "info.account",
      "info2.address.mobile.phone",
      "info2.contact.first"
    ]

could you please any help. thanks.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
};
var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);
function convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      const newPath = path ? path + '.' + key : key;
      const value = json[key];
      if (typeof value === 'object' &&
            !Array.isArray(value) &&
            value !== null && Object.keys(value).length>0) {
        convertJsonToArrayOfPathString(json[key], pathArray, newPath);
      }
      else {
        pathArray.push(newPath);
      }
    }
    return pathArray;
}

about 4 years ago · Juan Pablo Isaza Report

0

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);


function convertJsonToArrayOfPathString(json, pathArray = [], path = '') 
{
    Object.keys(json).forEach( key =>
    {
      const p = path ? path + '.' + key : key;
      var test = !json[key] || Object.keys( json[key]).length < 1 || Array.isArray( json[key]) ||
                typeof  json[key] !== 'object';
      return  test ? 
              pathArray.push(p) :
              this.convertJsonToArrayOfPathString(json[key], pathArray, p);
    });
    
    return pathArray;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!