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

145
Views
Get the parents from a json where string in javascript

I have this:

{ 'Payment' : {
   'Referenced' : 'referenced payment',
   'Conciliate' : 'conciliate payment',
   'Multiple' : 'multiple payment'
  }
}

but can change in all moment for random nodes or add more, like:

{ 'Payment' : {
   'Referenced' : 'referenced payment',
   'Conciliate' : 'conciliate payment',
   'Multiple' : {
       'mult1' : 'example1',
       'mult2' : 'example1'
  },
  'Inventory' : {
     'datastorage' : 'dt1'
  }
}

All nodes can be asigned randomly, and I need to search by value, I can pass:

referenced payment 

and need:

Payment/Referenced

or I send:

example1

and I need:

Payment/Multiple/mult1

I don't know if exist something like that.

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

0

// Function
const findPath = (obj, query) => {
  const makeArray = (obj, path = []) => {
    const pairs = Object.entries(obj);

    return pairs.map(([key, value]) =>
      typeof value === "object"
        ? makeArray(value, [...path, key])
        : { path: [...path, key], value }
    );
  };
  return (
    makeArray(obj)
      .flat(Infinity)
      .find(({ path, value }) => value === query)?.path.join("/") ?? null
  );
};


// Usage
const path1 = findPath(
  {
    Payment: {
      Referenced: "referenced payment",
      Conciliate: "conciliate payment",
      Multiple: {
        mult1: "example1",
        mult2: {
          test: 123,
        },
      },
      Inventory: {
        datastorage: "dt1",
      },
    },
  },
  123
);


const path2 = findPath(
  {
    Payment: {
      Referenced: "referenced payment",
      Conciliate: "conciliate payment",
      Multiple: {
        mult1: "example1",
      },
      Inventory: {
        datastorage: "dt1",
      },
    },
  },
  "referenced payment"
);

console.log("123: " + path1);
console.log("referenced payment: " + path2);

Explanation

The first step is converting the object into a linear array of the object tree and its paths. This is done recursively. Then, the array is flattened in order to be iterated through with Array.prototype.find, if the value matches the query, the path is returned, if no match was found it returns null.

Credits

Thanks to @Bravo for suggesting path array instead of template literal

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!