如何通过与模型比较将特定缺失字段添加到 json 对象

How to add specific missing fields to a json object by comparing with a model

我正在为我的应用程序使用 nodejs、couchdb。将来,如果我想检查我的文档是否与模型看起来一样,我正在考虑获取所有文档并与模型或模式进行比较,并将缺少的字段作为 null 添加到文档中并更新它.如何比较并找出具体缺失的字段并将其添加到文档中(json对象)。

Model/Schema

{
  "_id": "",
  "email": "",
  "password": "",
  "mobile": "",
  "username": "",
  "sharedNetworksArr": []
}

在我获取文档后,如果某些文档如下所示,我必须添加缺少的字段。

{
  "_id": "1",
  "email": "abc@gmail.com",
  "password": "abc",
  "username": "abc"
}

输出应该是,

{
  "_id": "1",
  "email": "abc@gmail.com",
  "password": "abc",
  "mobile": "",
  "username": "abc",
  "sharedNetworksArr": []
}

备注

All the documents may not miss the same field and some documents might be exact as the output.

您可以给 model.Or 中的默认值,您应该为其他字段添加必填字段。

 { "_id": "", "email": "", "password": "", "mobile": "", "username": "", 
 "sharedNetworksArr": {default:[]} }

您可以为此使用 lodash 的 defaultsdeep(或只是默认)库。

const _defaultsDeep = require('lodash.defaultsdeep');
let base = {
  "_id": "",
  "email": "",
  "password": "",
  "mobile": "",
  "username": "",
  "sharedNetworksArr": []
};
let input = {
  "_id": "1",
  "email": "abc@gmail.com",
  "password": "abc",
  "username": "abc"
};
let merged = _defaultsDeep(input, base);
console.log(JSON.stringify(merged, null, 2));

你可以像这样使用 ES6 扩展运算符

如果您使用的是单个对象

// Model/Scheme Object

const modelObject = {
    "_id": "",
    "email": "",
    "password": "",
    "mobile": "",
    "username": "",
    "sharedNetworksArr": []
};

// document returned object

const documentObject = {
    "_id": "1",
    "email": "abc@gmail.com",
    "password": "abc",
    "username": "abc"
};

/* 
    Get the result using ES6 spread operator by passing 
    the model object first, and the document object second 
*/

const outputObject = { ...modelObject, ...documentObject };

console.log(outputObject);

/* 
    { _id: '1',
    email: 'abc@gmail.com',
    password: 'abc',
    mobile: '',
    username: 'abc',
    sharedNetworksArr: []
    } 
*/

如果您使用的是对象数组。使用 Array 映射运算符转换结果

const documentObjectArray = [{
    "_id": "1",
    "email": "abc@gmail.com",
    "password": "abc",
    "username": "abc"
}, {
    "_id": "2",
    "email": "def@gmail.com",
    "password": "def",
    "username": "def"
}, {
    "_id": "3",
    "email": "ghi@gmail.com",
    "password": "ghi",
    "username": "ghi"
}];

const outputObjectArray = documentObjectArray.map((document) => {

    /*  transform result using ES6 spread operator by passing 
     the model object first, and the document object second */

    return { ...modelObject, ...document, }

});


console.log(outputObjectArray);
/*
    [ { _id: '1',
        email: 'abc@gmail.com',
        password: 'abc',
        mobile: '',
        username: 'abc',
        sharedNetworksArr: [] },
    { _id: '2',
        email: 'def@gmail.com',
        password: 'def',
        mobile: '',
        username: 'def',
        sharedNetworksArr: [] },
    { _id: '3',
        email: 'ghi@gmail.com',
        password: 'ghi',
        mobile: '',
        username: 'ghi',
        sharedNetworksArr: [] } ]
    */