1

I need to extract a value from a record using a path defined in a Array of strings. I came up with the following solution. It works, but this code seems a little bit too complicated to understand, in my opinion. I'd like to know if is there a better way to check if a value is a primitive type and if anyone can think in a simpler way to do the job.

const record = {
    firstName: "Joe Doe",
    personalData: {
        email: "[email protected]"            
    }
};
const path = ["personalData","email"];

const getJsonValueUsingPath = (record, path, index) => {
  const isPrimitiveType =
    Object(record[path[index]]) !== record[path[index]];
  if (isPrimitiveType) {
    return record[path[index]];
  } else {
    return getColumnValue(record[path[index]], path, index + 1);
  }
};
    

I need this function because I'm using a Third Party lib that requires such functionality. Please don't say it's a bad idea to extract an object property value using an array of strings.

2
  • "don't say it's a bad idea to extract a JSON value...": no, but it is a bad idea to call this JSON. Please... read the usage description of the json tag. This is not about JSON. There is no JSON in your question. I removed it from your question. Commented Dec 14, 2021 at 21:43
  • It could be as simple as const query = (ps) => (obj) => ps .reduce ((a, p) => (a || {}) [p], obj). Commented Dec 15, 2021 at 0:51

3 Answers 3

4

To simplify, you could remove the primitive-check and just assume that the path is correct and leads to the value that needs to be returned, no matter whether it is primitive or not.

Secondly, you can replace the loop with a reduce() call on the path.

const getValueUsingPath = (record, path) => 
    path.reduce((record, item) => record[item], record);

const record = {
    firstName: "Joe Doe",
    personalData: {
        email: "[email protected]"            
    }
};
const path = ["personalData","email"];

console.log(getValueUsingPath(record, path));

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is what you were after. It's only a little update to what you have, but it provides an alternate way to detect primitives

const record = {
  firstName: "Joe Doe",
  personalData: {
    email: "[email protected]"
  }
};
const path = ["firstName", "personalData", "email"];

let primitives = ['string', 'number', 'bigint', 'boolean', 'undefined', 'symbol', 'null'];

const getJsonValueUsingPath = (rec, pa, index) => {
  let item = rec[pa[index]];
  //console.log(typeof item)
  return primitives.includes((typeof item).toLowerCase()) ? item : getJsonValueUsingPath(item, path, index + 1)
}

console.log(getJsonValueUsingPath(record, path, 0));
console.log(getJsonValueUsingPath(record, path, 1));

Comments

0

lodash if you don't mind:

const _ = require('lodash');
const record = { firstName: "Joe Doe", personalData: { email: "[email protected]" } };
const path = ["personalData","email"];

_.get(record, path); // '[email protected]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.