LisaN
LisaN

Reputation: 165

How to access a certain field inside Nested Documents

I have this JSON FILE

{
  "_id": "GgCRguT8Ky8e4zxqF",
  "services": {
    "notifications": [
      {
        "_id": "5hqPb76VhiwJ7Y86q5",
        "work": true,
        "maried": true
      },
      {
        "_id": "4ds4d654sd65d7zW45",
        "work": false,
        "married": true
      }
    ],
    "profile": {
      "name": "Janis"
    }
  }
}

I want to get the object inside the notification array but couldn't figure out how to do it

exemple: Is there a way to do it with a function by entering the _id : 4ds4d654sd65d7zW45 i want to get this

{
  "_id": "4ds4d654sd65d7zW45",
  "work": false,
  "married": true
}

Couldn't figure out how to access the notifications and get what i want by entering the id

Could you please help me ?

Upvotes: 1

Views: 46

Answers (3)

Nishant Dixit
Nishant Dixit

Reputation: 5522

You can use filter function of Array for this.

var data = {
    "_id": "GgCRguT8Ky8e4zxqF",
    "services": {
        "notifications": [{
                "_id": "5hqPb76VhiwJ7Y86q5",
                "work": true,
                "maried": true
            },
            {
                "_id": "4ds4d654sd65d7zW45",
                "work": false,
                "married": true
            }
        ],
        "profile": {
            "name": "Janis"
        }
    }
}

function getNotificationById(id) {
    return data.services.notifications.filter(({
        _id
    }) => _id == id)[0]
}

console.log(getNotificationById("4ds4d654sd65d7zW45"));

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use Array.find() inside the function:

USING ES6

var obj = {
  "_id": "GgCRguT8Ky8e4zxqF",
  "services": {
    "notifications": [{
        "_id": "5hqPb76VhiwJ7Y86q5",
        "work": true,
        "maried": true
      },
      {
        "_id": "4ds4d654sd65d7zW45",
        "work": false,
        "married": true
      }
    ],
    "profile": {
      "name": "Janis"
    }
  }
}

function findObj(id) {
  return obj.services.notifications.find(({_id}) => _id === id);
}
console.log(findObj('4ds4d654sd65d7zW45'));

USING PLAIN FUNCTION (works for older browsers and IE)

var obj = {
  "_id": "GgCRguT8Ky8e4zxqF",
  "services": {
    "notifications": [{
        "_id": "5hqPb76VhiwJ7Y86q5",
        "work": true,
        "maried": true
      },
      {
        "_id": "4ds4d654sd65d7zW45",
        "work": false,
        "married": true
      }
    ],
    "profile": {
      "name": "Janis"
    }
  }
}

function findObj(id) {
  return obj.services.notifications.find(function(obj){
    return obj._id === id;
  });
}
console.log(findObj('4ds4d654sd65d7zW45'));

Upvotes: 1

AdityaParab
AdityaParab

Reputation: 7100

Yes. You can use Array.prototype.find method.

const json = {
  "_id": "GgCRguT8Ky8e4zxqF",
  "services": {
    "notifications": [
      {
        "_id": "5hqPb76VhiwJ7Y86q5",
        "work": true,
        "maried": true
      },
      {
        "_id": "4ds4d654sd65d7zW45",
        "work": false,
        "married": true
      }
    ],
    "profile": {
      "name": "Janis"
    }
  }
};

function getNotificationById(json, id) {
  return json.services.notifications.find(n => n._id === id);
}

getNotificationById(json, '4ds4d654sd65d7zW45');

Upvotes: 0

Related Questions