bjskistad bjskistad - 4 months ago 14
JSON Question

Parsing JSON with an optional object?

I have the following script:

var argv = process.argv.splice(2);
var obj = argv[0];
var pkg = require("./package.json");

console.log(pkg.obj);


I want my script to parse for whatever
obj
equals, not
obj
itself. Is there any way to do this?

mmm mmm
Answer

To do what you want, you can use bracket notation:

console.log(pkg[obj]);