1

Is it possible to console.log something like this:

myParent.myChildData(5) (variable literal name + value in brackets)

from a JSON object such as this:

{myParent: {myChildData: 5}}

I would like to do it with referencing the object notation ideally only once. Something like:

console.log(printExpression(myParent.myChildData))

Where printExpression I'm certainly happy to be a generic helper function that could return this. I've searched high and low, but obviously printExpression receives the actual evaluated value and this causes a road block.

2
  • Maybe: how to get object's name in javascript? Commented Jan 8, 2020 at 3:58
  • Yes the name of the variable, as the text on the link says is irretrievably lost. So don't think it will bed possible. Thanks for sharing that. Commented Jan 9, 2020 at 3:18

1 Answer 1

1

You can turn JSON into a JavaScript object by using JSON.parse(jsonString).

You can store that as a variable and then console.log it.

Or you can just directly console.log the passed data like this:

console.log(JSON.parse('{"myparent":{"myChildData": 5}}').myParent.myChildData);

Edit

After understanding what exactly the helper function does, I've created a printExpression function that returns string values based on your example.

function printExpression(object, stringBefore) {
  //Recursively make objects with keys as methods
  let newObject = {};

  for (var key in object) {
    //Make sure the key exists on the object
    if (object.hasOwnProperty(key)) {
      let value = object[key];
      //If the value is an object, just add a get method that returns the object
      if (typeof(value) == "object") {
        let childObject = printExpression(value, key + ".");
        newObject[key] = childObject;
      }
      //If not, make a method that returns the wanted syntax
      else {
        //Form the string based on specific syntax
        let str = key + "(" + value + ")";
        //Check if we should add stringBefore
        if (stringBefore) {
          str = stringBefore + str;
        }
        newObject[key] = str;
      }
    }
  }
  //Return the new object
  return newObject;
}

var example = printExpression(JSON.parse('{"myParent": {"myChildData": 5}}'));
console.log(example.myParent.myChildData);

How It Works When creating the helper object, it recursively reads all the keys of the original object and makes a new object that returns the keys in an organized way. For example if the original object was { greeting: "hello" } then newObject.greeting would be "greeting(hello)" (as you said it should be).

Possible Problems Doesn't get updated when you change the original object. I don't think this will be much of a problem as you seem to be reading static JSON data, but just letting you know.

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

4 Comments

Thanks, but you doubled up on specifying myparent and myChildData. So that won't fly. Cheers anyway.
Can you please explain what exactly you're trying to do? Do you want to make a helper function that would return 5 (based on the example you gave)?
I want to print this. Not the result of it, but literally: myParent.myChildData(5)
Would something like console.log(helper[myparent].myChildData]) be ok? If yes, then I know a way of making your helper function.

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.