how to push the object keys and values to array

I need to push the key value pair to new array and should build the mongodb $match query.

const queryFormat = (payload) => {
    delete payload.userEventId
    var query = {}
     var res = []
    for(key in payload) {
       res.push( {key:payload[key]})
    }
    query['$match'] = {"$and" :res}
  console.log(query)
    
}


const payload = {
    id :1,
    name : 'Alfred',
    location : 'moon'
}
queryFormat(payload)

expected output

{"$match":{"$and":[{id:1},{name:"Alfred"},{location:"moon"}]}}

output I got

{"$match":{"$and":[{key:1},{key:"Alfred"},{key:"moon"}]}}

How to deal with it
Thanks!!

17 thoughts on “how to push the object keys and values to array”

  1. Unquestionably believe that which you stated. Your favorite justification seemed to be on the net the easiest thing to
    be aware of. I say to you, I definitely get irked while people consider worries that they
    plainly do not know about. You managed to hit the
    nail upon the top and also defined out the whole thing without having side-effects , people can take a signal.

    Will probably be back to get more. Thanks http://herreramedical.org/azithromycin

    Reply

Leave a Comment