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

132
Views
Convert object to array of prorperties

I need to convert object:

{
        middleName: null,
        name: "Test Name",
        university: {
            country: {
                code: "PL"
            },
            isGraduated: true,
            speciality: "Computer Science"
        }
    }

to array:

[{
        key: "name",
        propertyValue: "Test Name",
    },
    {
        key: "middleName",
        propertyValue: null,
    },
    {   
        key: "university.isGraduated",
        propertyValue: true,
    },
    {   
        key: "university.speciality",
        propertyValue: "Computer Science", 
    },
    {   
        key: "university.country.code",
        propertyValue: "PL"
    }];

I wrote algorithm, but it's pretty dummy, how can I improve it? Important, if object has nested object than I need to write nested object via dot (e.g university.contry: "value")

let arr = [];
    Object.keys(parsedObj).map((key) => {
        if (parsedObj[key] instanceof Object) {
            Object.keys(parsedObj[key]).map((keyNested) => {
                if (parsedObj[key][keyNested] instanceof Object) {
                    Object.keys(parsedObj[key][keyNested]).map((keyNestedNested) => {
                        arr.push({ 'key': key + '.' + keyNested + '.' + keyNestedNested, 'propertyValue': parsedObj[key][keyNested][keyNestedNested] })
                    })
                } else {
                    arr.push({ 'key': key + '.' + keyNested, 'propertyValue': parsedObj[key][keyNested] })
                }
            })
        } else {
            arr.push({ 'key': key, 'propertyValue': parsedObj[key] })
        }
    });

Thanks

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

0

A recursive function implementation.

I have considered that your object consist of only string and object as the values. If you have more kind of data types as your values, you may have to develop on top of this function.

const myObj = {
  middleName: null,
  name: "Test Name",
  university: {
    country: {
      code: "PL"
    },
    isGraduated: true,
    speciality: "Computer Science"
  }
}
const myArr = [];

function convertObjectToArray(obj, keyPrepender) {
  Object.entries(obj).forEach(([key, propertyValue]) => {
    if (typeof propertyValue === "object" && propertyValue) {
      const updatedKey = keyPrepender ? `${keyPrepender}.${key}` : key;
      convertObjectToArray(propertyValue, updatedKey)
    } else {
      myArr.push({
        key: keyPrepender ? `${keyPrepender}.${key}` : key,
        propertyValue
      })
    }
  })
}

convertObjectToArray(myObj);
console.log(myArr);

about 4 years ago · Juan Pablo Isaza Report

0

I wrote a small version using recursive function and another for validation is an object.

let values = {
  middleName: null,
  name: "Test Name",
  university: {
    country: {
      code: "PL"
    },
    isGraduated: true,
    speciality: "Computer Science"
  }
}

function isObject(obj) {
  return obj != null && obj.constructor.name === "Object"
}

function getValues(values) {
  let arrValues = Object.keys(values).map(
    v => {
      return { key: v, value: isObject(values[v]) ? getValues(values[v]) : values[v] };

    });
  console.log(arrValues);
}

getValues(values);

about 4 years ago · Juan Pablo Isaza Report

0

This is the most bulletproof I could do :D, without needing a global variable, you just take it, and us it wherever you want!

const test = {
  middleName: null,
  name: "Test Name",
  university: {
    country: {
      code: "PL"
    },
    isGraduated: true,
    speciality: "Computer Science"
  }
};


function toPropertiesByPath(inputObj) {
  let arr = [];
  let initialObj = {};

  const getKeys = (obj, parentK='') => {
    initialObj = arr.length === 0 ? obj: initialObj;
    const entries = Object.entries(obj);
    for(let i=0; i<entries.length; i++) {
      const key = entries[i][0];
      const val = entries[i][1];
      const isRootElement = initialObj.hasOwnProperty(key);
      parentK = isRootElement ? key: parentK+'.'+key;

      if(typeof val === 'object' && val!==null && !Array.isArray(val)){
        getKeys(val, parentK);
      } else {
       arr.push({ key: parentK, property: val });
      }
    }
  };

  getKeys(inputObj);
  return arr;
}

console.log(toPropertiesByPath(test));

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!