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.

GONeale
  • 26,302
  • 21
  • 106
  • 149
  • Maybe: [how to get object's name in javascript?](https://stackoverflow.com/questions/5448135/how-to-get-objects-name-in-javascript) – Tân Jan 08 '20 at 03: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. – GONeale Jan 09 '20 at 03:18

1 Answers1

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.

programmerRaj
  • 1,810
  • 2
  • 9
  • 19
  • Thanks, but you doubled up on specifying myparent and myChildData. So that won't fly. Cheers anyway. – GONeale Jan 09 '20 at 03:17
  • 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)? – programmerRaj Jan 09 '20 at 22:26
  • I want to print this. Not the result of it, but literally: `myParent.myChildData(5)` – GONeale Jan 09 '20 at 23:03
  • Would something like `console.log(helper[myparent].myChildData])` be ok? If yes, then I know a way of making your helper function. – programmerRaj Jan 11 '20 at 01:20