Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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": "[email protected]",
  "password": "abc",
  "username": "abc"
}

The output should be as,

{
  "_id": "1",
  "email": "[email protected]",
  "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.

like image 943
Venura Nimesh Avatar asked Sep 04 '19 06:09

Venura Nimesh


People also ask

How to compare 2 Json objects?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .


1 Answers

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": "[email protected]",
    "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: '[email protected]',
    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": "[email protected]",
    "password": "abc",
    "username": "abc"
}, {
    "_id": "2",
    "email": "[email protected]",
    "password": "def",
    "username": "def"
}, {
    "_id": "3",
    "email": "[email protected]",
    "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: '[email protected]',
        password: 'abc',
        mobile: '',
        username: 'abc',
        sharedNetworksArr: [] },
    { _id: '2',
        email: '[email protected]',
        password: 'def',
        mobile: '',
        username: 'def',
        sharedNetworksArr: [] },
    { _id: '3',
        email: '[email protected]',
        password: 'ghi',
        mobile: '',
        username: 'ghi',
        sharedNetworksArr: [] } ]
    */
like image 60
Grey Avatar answered Oct 01 '22 14:10

Grey