• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

141
Views
using the value from one JSON as the key in another

I have 2 JSONs. One contains data like:

const data = {
"dataElements": {
    "someField": "someData1",
    "someOtherField": "someOtherData"
},
"anotherDataElement": {
    "yetMoreFields": "evenMoreData",
    "stillMore": "dater"
}}

The other contains something like:

const lookUp = {
"lookUpValues": {
    "valueOne": "data.dataElements.someField",
    "someOtherField": "someOtherData"
}}

What I need to do is take the value of lookup.lookUpValues.valueOne (which would resolve to "data.dataElements.someField") and get that value from the data object ("someData1")

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

JavaScript or Node.js does not provide any function to achieve your goal but you can write such a function yourself in several lines of code:

function getValue(object, path) {
  // split the path in pieces separated by '.'
  const pieces = path.split('.');

  // walk the path, for each piece get the property with the same name from the current object
  return pieces.reduce(
    // result = the current object being walked
    // piece = the name of the current path piece
    // return the `piece` property of the `result` object (or `undefined` if it is not an object)
    (result, piece) => result instanceof Object ? result[piece] : undefined,
    // start walking with the provided object
    object
  );
}

Then call it like:

const value = getValue(data, 'dataElements.someField');

Check it online.

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error