Monday 10 April 2017

Declaring dynamic keys in json (javascript, node)

I have an array of objects, each object is similar to:

{ word: 'intentional',
  definition: 'done by intention or design',
  type: 'adjective',
  Synonyms: [  'conscious', 'deliberate', 'intended', 'knowing', ] }

I am trying to convert the whole array into following json format:

{
    "conscious": {
        "data": ["done by intention or design"],
        "type": "adjective",
        "Synonym For": ["intentional"]
    },
    "deliberate": {
        "data": ["done by intention or design"],
        "type": "adjective",
        "Synonym For": ["intentional"]
    },
    ...

}

This json format is an input to another program, which I do not control. I am running it on node.js.

How can I declare an object and then loop through the array to fill it as intended?



via Gaurav N

No comments:

Post a Comment