-3

I am having the result set from DB as

{
    "scrapped_name": [
        "harry mccracken"
    ],
    "publications": [
        {
            "_id": "5d3021a6eedfed29a7b5ae75",
            "name": "Fast Company"
        },
        {
            "_id": "5d3021a6eedfed1728b5ae7c",
            "name": null
        }
    ],
    "language": [],
    "location": [],
    "_id": "5d3021a6eedfed37d3b5ae88",
    "name": "harry mccracken",
    "createdAt": "2019-07-18T07:37:10.626Z",
    "updatedAt": "2019-07-18T07:37:10.626Z",
    "social_media": []
}

1) I need to perform some activity on result data set and then I need to make the result set same as the above but this time I need to make it manually.I am facing issue while making the Publication array inside the object. 2) Can someone help me for pushing the new key value in my result set. like I have the above result from the DB query now I want to push the new key like :

publisher : true

So the resultant array I want in my second question is:

{
    "scrapped_name": [
        "harry mccracken"
    ],
    "publications": [
        {
            "_id": "5d3021a6eedfed29a7b5ae75",
            "is_followed": true,
            "name": "Fast Company"
        },
        {
            "_id": "5d3021a6eedfed1728b5ae7c",
            "is_followed": false,
            "name": null
        }
    ],
    "language": [],
    "location": [],
    "_id": "5d3021a6eedfed37d3b5ae88",
    "name": "harry mccracken",
    "createdAt": "2019-07-18T07:37:10.626Z",
    "updatedAt": "2019-07-18T07:37:10.626Z",
    "social_media": [],
    "publisher": true
}

Above Is followed will be dynamically calculated from other table in runtime.

NARGIS PARWEEN
  • 1,489
  • 3
  • 16
  • 26

1 Answers1

0

         const object = {
    scrapped_name: [
      'harry mccracken',
    ],
    publications: [
      {
        _id: '5d3021a6eedfed29a7b5ae75',
        name: 'Fast Company',
      },
      {
        _id: '5d3021a6eedfed1728b5ae7c',
        name: null,
      },
    ],
    language: [],
    location: [],
    _id: '5d3021a6eedfed37d3b5ae88',
    name: 'harry mccracken',
    createdAt: '2019-07-18T07:37:10.626Z',
    updatedAt: '2019-07-18T07:37:10.626Z',
    social_media: [],
  };
  
  object.publisher = true; // Added a key `publisher`
  // Customise `publications` array
  if (object.publications && Array.isArray(object.publications)) {
    object.publications.forEach((f) => {
      const referenceObj = f;
      let hasFollow = false;
      if (f._id && f._id === '5d3021a6eedfed29a7b5ae75') {
        hasFollow = true;
      }
      referenceObj.is_followed = hasFollow;
    });
  }
  
  console.log(object);
Neel Rathod
  • 2,013
  • 12
  • 28