-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.

2
  • Loop through the publications array and add is_followed to each object? Commented Aug 26, 2019 at 9:30
  • data['publisher'] = true Commented Aug 26, 2019 at 9:30

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I am also using object.publisher = true, but it is not adding the value into the object. Please help me to add the value to the Object.
Please paste your object here
Try this in chrome's console ``` const obj = {}; obj.a = 'A'; console.log(obj); ```

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.