-1

This is processed data(INPUT).

[['dObjectData.question':{ '$regex': '^(abc)', '$options': 'i'}],
 ['dObjectData.answer': { '$regex': '.*(pqr).*','$options': 'i' }]]

I want to make(modified this process data) in this format(OUTPUT).

[{'dObjectData.question':{'$regex':'^(abc)','$options':'i'},
   'dObjectData.answer':{'$regex': '.*(pqr).*', '$options':'i'}]
Rahul Saini
  • 927
  • 1
  • 11
  • 28
  • 4
    Your input seems invalid javascript. Should the `:` between key and value be a `,`? – user3297291 Aug 23 '18 at 07:48
  • Possible duplicate of [How to create an object from an Array of key-value pairs?](https://stackoverflow.com/questions/20059995/how-to-create-an-object-from-an-array-of-key-value-pairs) – t.niese Aug 23 '18 at 07:51

1 Answers1

0

You can try like that:

x = [
    [
        'dObjectData.question', {
            '$regex': '^(abc)',
            '$options': 'i'
        }
    ],
    [
        'dObjectData.answer', {
            '$regex': '.*(pqr).*',
            '$options': 'i'
        }
    ]
]
o = x.reduce(function (p, c) {
    p[c[0]] = c[1];
    return p;
}, [])
console.log(o)

The output like that:

[ 'dObjectData.question': { '$regex': '^(abc)', '$options': 'i' },
  'dObjectData.answer': { '$regex': '.*(pqr).*', '$options': 'i' } ]
youDaily
  • 1,372
  • 13
  • 21
  • Your answer is *almost* identical to [this answer](https://stackoverflow.com/a/30656472/3297291) from the question linked in the comments, but includes a mistake. Your output is invalid, and the seed for the `reduce` has to be an object. You might want to fix it to `o = [ x.reduce( ... , { } ) ];` – user3297291 Aug 23 '18 at 08:29