Alex
Alex

Reputation: 323

How to use find() method on an Object within an Object

I'm trying to return the object within comments_text that has the key that matches findKey.

const findKey = "-MkeQEa2sgCho_ijk7TO"

const posts = [
    0:{
        comment: false,
        comments_text: {
            0: {
                commenter_comment: "test1",
                commenter_handle: "LEOPARD1",
                commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
                key: "-MkeQEa2sgCho_ijk7TO"
            },
            1: {
                commenter_comment: "test2",
                commenter_handle: "LEOPARD1",
                commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
                key: "-MkeQM4yD95PSEPfAj4v"
            }
        },
        handle: "username1",
        key: "-MkePAHv8kux6INOSCji",
        title: "Hello"
      }
]

As an example, in this case, since findKey is "-MkeQEa2sgCho_ijk7TO", then this would be returned:

            {
                commenter_comment: "test1",
                commenter_handle: "LEOPARD1",
                commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
                key: "-MkeQEa2sgCho_ijk7TO"
            }

I have tried the following...

const returnMatchingObject = posts.comments_text.find(object=> object.key === findKey);

However, I'm receiving an error message of "Cannot read properties of undefined (reading 'find')"

I feel like I'm accessing the Object incorrectly. How can I improve this? Thank you for reviewing and for any help.

Upvotes: 0

Views: 66

Answers (2)

Drew Reese
Drew Reese

Reputation: 202618

Similar to other answer, looping over the posts array to find the matching nested comments_text object property. Difference is it breaks out of the loop at the first found match and doesn't require the post.comments_text property to be an array first.

let item;

for (const post of posts) {
  item = Object.values(post.comments_text).find(
    (comment) => comment.key === findKey
  );

  if (item) {
    break;
  }
}

const findKey = "-MkeQEa2sgCho_ijk7TO";

const posts = [
  {
    comment: false,
    comments_text: {
      0: {
        commenter_comment: "test1",
        commenter_handle: "LEOPARD1",
        commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
        key: "-MkeQEa2sgCho_ijk7TO"
      },
      1: {
        commenter_comment: "test2",
        commenter_handle: "LEOPARD1",
        commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
        key: "-MkeQM4yD95PSEPfAj4v"
      }
    },
    handle: "username1",
    key: "-MkePAHv8kux6INOSCji",
    title: "Hello"
  }
];

let item;

for (const post of posts) {
  item = Object.values(post.comments_text).find(
    (comment) => comment.key === findKey
  );

  if (item) {
    break;
  }
}

console.log(item);

Upvotes: 1

dave
dave

Reputation: 2901

You need to loop through your posts (assuming there's going to be more than one post in your array).

Your comments_text also needs to be an array of objects. Once you're in the loop you can search within it to find your key:

const findKey = "-MkeQEa2sgCho_ijk7TO";

const posts = [{
  comment: false,
  comments_text: [{
      commenter_comment: "test1",
      commenter_handle: "LEOPARD1",
      commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
      key: "-MkeQEa2sgCho_ijk7TO"
    },
    {
      commenter_comment: "test2",
      commenter_handle: "LEOPARD1",
      commenter_uid: "ErGGzFxr4oPjWCEAPuNpdTdzPu63",
      key: "-MkeQM4yD95PSEPfAj4v"
    }
  ],
  handle: "username1",
  key: "-MkePAHv8kux6INOSCji",
  title: "Hello"
}]


posts.forEach((post) => {
  const returnMatchingObject = post.comments_text.find(object => object.key === findKey);

  console.log(returnMatchingObject);
})

Upvotes: 2

Related Questions