Search code examples
javascriptnode.jsminimist

Javascript unpack command line dictionary


I am learning javascript.

I am recieving a variable length of dictionary from the command line of the form

--myargs = {"foo":"bar", "foo2":"bar2"}

I can read the args by

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv)
var myargs = argv["myargs"] 

I need to unpack the dictionary myargs like this->

my_new_args= {Key: "foo", Value: "bar", Key: "foo2", Value: "bar2" }; 

How do I do this in JS?


Solution

  • You can use map() on object keys and return array of objects.

    var myargs = {"foo":"bar", "foo2":"bar2"}
    var result = Object.keys(myargs).map(e => ({Key: e, Value: myargs[e]}));
    console.log(result)