Locate object with specific properties for two nested objects

Hi there! I’m trying to locate an object in the JSON response that has specific properties for two nested objects.

Here’s a snippet of the response

{
    "data": [
        {
            "type": "dailyLessonAssignment",
            "id": "535",
            "attributes": {
                "userId": 212671,
                "date": "2021-05-08",
                "lessonAssignmentsMetadata": [
                    {
                        "lId": 2302126,
                        "status": 0,
                        "seq": 15
                    },
                    {
                        "lId": 2302120,
                        "status": 0,
                        "seq": 16
                    },
                    {
                        "lId": 2302124,
                        "status": 0,
                        "seq": 17
                    }
                ]
            },

I am able to locate the object by using the find command for 1 nested object, date:

const jsonData = pm.response.json();
const date = jsonData.data.find(u => u.attributes.date === "2021-05-08"); 
console.log(date);

But I’d like to locate the object by date and userId. (The response can have multiple users with the same date.)

I’ve tried the following but it returned undefined:

const date2 = _.find(jsonData.data.attributes, { 
    "userId": 212671,
    "date": "2021-05-08"
});
console.log(date2);

Appreciate your help!

Have you tried using the AND (&&) operator?

Thanks for your reply, Valentin (btw, I’m a big fan!).

I had difficulty with the article, and tried the following but it’s not right.

const jsonData = pm.response.json();
const user = jsonData.data.find(u => u.attributes.userId === 212618); 
const date = jsonData.data.find(u => u.attributes.date === '2021-05-08');
const assignment = user && date;
console.log(assignment);

In the console, I see the date is right, but the userId isn’t:

{type: "dailyLessonAssignment", id: "535", attributes: {…}…}
type: "dailyLessonAssignment"
id: "535"
attributes: {…}
userId: 212671
date: "2021-05-08"
lessonAssignmentsMetadata: [3]

Would you guide me further? Appreciate your time.

The condition is inside the find function.

So try something like:

u => u.attributes.userId === 212618 && u.attributes.date === '2021-05-08'

1 Like

That works perfectly! Thank you, Valentin :smiley: