如何更改Javascript map()函数中元素的值?

时间:2019-03-12 05:46:48

标签: javascript ecmascript-6 mapreduce

我有一个查询数据库并返回用户对象集合的代码段:

users: () => User
      .find()
      .then(users => users.map(user => user._doc))
      .catch((err) => { throw err; }),

很明显,每个用户对象都包含字段,例如用户名,名字,密码等。但是,我想在返回对象之前将null分配给密码字段(user._doc )。为此,我尝试了以下操作:

.then(users => users.map(user => { ...user._doc, password: null }))

但是由于语法错误而失败。有帮助吗?

示例数据:

{
  "data": {
    "users": [
      {
        "_id": "5c8664161b81d506e5c98e98",
        "firstName": "Bill",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$m57TfbtPVGS5xnJ6PORO9A$G4cvgac/wdtnra0+st+VWTplFNL+6B5zCrGovdWh7jOGAc2el9kVRg",
        "username": "gates",
        "apiKey": "5c8664161b81d506e5c98e97",
        "privileges": [
          "GUEST",
          "AUTHOR",
          "EDITOR"
        ]
      },
      {
        "_id": "5c8666e132dfae0802bb855b",
        "firstName": "Jim",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$TGImmObOvhKN4L/gYHMnsg$6ymjG5iwXCgKkm5VL+jJs5qyqDKsMpSL3oJbg4sJaDHk1z+v1jtVQA",
        "username": "jimm",
        "apiKey": "5c8666e132dfae0802bb855a",
        "privileges": [
          "GUEST",
          "AUTHOR",
          "EDITOR"
        ]
      },
      {
        "_id": "5c873f9fd925640b0460e23b",
        "firstName": "Tom",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$IHIUzO7eFjzVSxJa1aeIsQ$Z2LkCnBZCSutTTq0WLmF8KB8p/HJsvuPP2JnaVwzoYLyVh9/9bh6EA",
        "username": "tom",
        "apiKey": "5c873f9fd925640b0460e23a",
        "privileges": [
          "OWNER"
        ]
      }
    ]
  }
}

0 个答案:

没有答案