Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda