Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Cómo usar el método find () en un objeto dentro de un objeto

Estoy tratando de devolver el objeto dentro de comments_text que tiene la clave que coincide con 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" } ]

Como ejemplo, en este caso, dado que findKey es "-MkeQEa2sgCho_ijk7TO", se devolvería esto:

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

He probado lo siguiente...

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

Sin embargo, recibo un mensaje de error de "No se pueden leer las propiedades de undefined (leyendo 'buscar')"

Siento que estoy accediendo al Objeto incorrectamente. ¿Cómo puedo mejorar esto? Gracias por revisar y por cualquier ayuda.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe recorrer sus publicaciones (suponiendo que habrá más de una publicación en su matriz).

Su texto de comments_text también debe ser una matriz de objetos. Una vez que esté en el bucle, puede buscar dentro de él para encontrar su clave:

 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 Report

0

Similar a otra respuesta, recorriendo la matriz de posts para encontrar la propiedad del objeto de texto de comments_text anidados coincidentes. La diferencia es que sale del bucle en la primera coincidencia encontrada y no requiere que la propiedad post.comments_text sea una matriz primero.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!