简体   繁体   中英

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

I'm using nodejs, couchdb for my application. In the future, if I want to check whether my documents look like the same as a model, I'm thinking to fetch all the documents and compare with the model or a schema and add the missing field to the document as null and update it. How to compare and find out the specific missing field and add it to the document(json object).

Model/Schema

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

After I have fetched my documents and if some of the documents look like below, I have to add the missing fields.

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

The output should be as,

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

Note

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

You could use ES6 spread operator like this

If you are using a single object

// 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: []
    } 
*/

If you are using an array of objects. Use the Array map operator to transform the result

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: [] } ]
    */

You can use lodash's defaultsdeep (or just defaults) library for this.

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));

You can give default value in to the model.Or you should add required field for other.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM