Javascript 如何正确分配对象属性是一个问题

Javascript 如何正确分配对象属性是一个问题,javascript,ecmascript-6,Javascript,Ecmascript 6,我有以下目标: { "_id": "583c4e054c99d310f543b11e", "cellphone": "123456", "password": "$2a$10$d3SeD5CCzTo6wBR/4SGUu.i7vUvX98N1NlpBTwDdWCRrIYcVwWmCO", "fullname": "some name", "gender": "male", "avatar": { "_id": "583c4e054c99d310f543b11d",

我有以下目标:

{
  "_id": "583c4e054c99d310f543b11e",
  "cellphone": "123456",
  "password": "$2a$10$d3SeD5CCzTo6wBR/4SGUu.i7vUvX98N1NlpBTwDdWCRrIYcVwWmCO",
  "fullname": "some name",
  "gender": "male",
  "avatar": {
    "_id": "583c4e054c99d310f543b11d",
    "__v": 0,
    "url": "",
    "likesCount": 0,
    "description": "Hey guys"
  },
  "__v": 0
}
在下一段代码中,我想创建一个新对象,不使用mongodb(_v)中的属性,并将_id重命名为id

  let plainUser = {
    id: user._id,
    ...user.toObject(),
  }

  delete plainUser._id;
  delete plainUser.password;
  delete plainUser.__v;
  delete plainUser.avatar._id;
  delete plainUser.avatar.__v;

  return res.send(plainUser);

我想我可以做得与众不同。关于如何使用ES6正确改进代码的任何建议?

最好明确说明要保留哪些属性,而不是指定要删除哪些属性。而且
delete
效率很低。那就去买吧

return res.send({
    id: user._id,
    cellphone: user.cellphone,
    fullname: user.fullname,
    gender: user.gender,
    avatar: {
        url: user.avatar.url,
        likesCount: user.avatar.likesCount,
        description: user.avatar.description
    }
});
对于一些简化,请从以下方面了解该方法:


最好明确说明要保留哪些属性,而不是指定要删除哪些属性。而且
delete
效率很低。那就去买吧

return res.send({
    id: user._id,
    cellphone: user.cellphone,
    fullname: user.fullname,
    gender: user.gender,
    avatar: {
        url: user.avatar.url,
        likesCount: user.avatar.likesCount,
        description: user.avatar.description
    }
});
对于一些简化,请从以下方面了解该方法:


下面是一个将规范化对象属性的函数:

function normalizeObjectKeys(obj) {
    let normilizedObj = {};
    Object.keys(obj).map(key => {
        let newKey = key.replace(/_+/, '');
        normilizedObj[newKey] = ('object' !== typeof obj[key]) ? obj[key] : normalizeObjectKeys(obj[key]);
    });
    return normilizedObj;
}
例如:

normalizeObjectKeys({
  "_id": "583c4e054c99d310f543b11e",
  "cellphone": "123456",
  "password": "$2a$10$d3SeD5CCzTo6wBR/4SGUu.i7vUvX98N1NlpBTwDdWCRrIYcVwWmCO",
  "fullname": "some name",
  "gender": "male",
  "avatar": {
    "_id": "583c4e054c99d310f543b11d",
    "__v": 0,
    "url": "https://pp.vk.me/c631525/v631525614/2b398/kbI5QohgEgQ.jpg",
    "likesCount": 0,
    "description": "Hey guys"
  },
  "__v": 0
});

下面是一个将规范化对象属性的函数:

function normalizeObjectKeys(obj) {
    let normilizedObj = {};
    Object.keys(obj).map(key => {
        let newKey = key.replace(/_+/, '');
        normilizedObj[newKey] = ('object' !== typeof obj[key]) ? obj[key] : normalizeObjectKeys(obj[key]);
    });
    return normilizedObj;
}
例如:

normalizeObjectKeys({
  "_id": "583c4e054c99d310f543b11e",
  "cellphone": "123456",
  "password": "$2a$10$d3SeD5CCzTo6wBR/4SGUu.i7vUvX98N1NlpBTwDdWCRrIYcVwWmCO",
  "fullname": "some name",
  "gender": "male",
  "avatar": {
    "_id": "583c4e054c99d310f543b11d",
    "__v": 0,
    "url": "https://pp.vk.me/c631525/v631525614/2b398/kbI5QohgEgQ.jpg",
    "likesCount": 0,
    "description": "Hey guys"
  },
  "__v": 0
});
{…user.toObject(),}
无效ES6。
{…user.toObject(),}
无效ES6。