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

153
Vistas
For in loop on object, and .find, returning "undefined"

I'm trying to loop through an object (objectToLoop) and check to see if if contains the key comments_text. If it does, I want to check comments_text, and if the key within comments_text matches commentId, I want to set let item to the entire object within coments_text.

I'm trying to set let item to the following, but I'm getting "undefined":

{
 commenter_comment: "reedwe",
 commenter_handle: "FALCON1",
 commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
 key: "-MlaKXBMXzXIBJMsZQNX"
}

Advice and feedback appreciated.

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

function returnMatchingComment(commentIdKey) {
  let item;
  for (const post in objectToLoop) {
    if (post.comments_text) {
      item = Object.values(post.comments_text).find(
        (comment) => comment.key === commentIdKey
      )
    };
    if (item) {
      break;
    }
  }
  return item
}
console.log(
  returnMatchingComment(commentId)
) 

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

0

You do not need to use the for...in loop. You can first check if objectToLoop has the property comments_text by using Object.prototype.hasOwnProperty() and check if objectToLoop?.comments_text?.key == commentId. I used ?. (optional chaining) so that if objectToLoop.comments_text didn't exist, it would just return undefined and not throw an ugly error.
Example:

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}
if(objectToLoop.hasOwnProperty('comments_text') && objectToLoop?.comments_text?.key == commentId) { 
    let item = objectToLoop.comments_text;
    console.log(item)
}
To be honest, we don't even need objectToLoop.hasOwnProperty('comments_text') as with optional chaining (as mentioned earlier on), objectToLoop?.comments_text?.key == commentId would just return undefined if objectToLoop.comments_text did not exist or false if objectToLoop.comments_text.key was not equal to commentId.
Example 2:

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

if(objectToLoop?.comments_text?.key == commentId) { 
    let item = objectToLoop.comments_text;
    console.log(item)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can put some debug.Your if never fire. The 'i' log gives you a big information =)

const commentId = "-MlaKXBMXzXIBJMsZQNX"

const objectToLoop = {
  comment: false,
  comments_text: {
    commenter_comment: "reedwe",
    commenter_handle: "FALCON1",
    commenter_uid: "ZALb0B3wZEWjfdFIUOxteJRl9Xu1",
    key: "-MlaKXBMXzXIBJMsZQNX"
  },
  handle: "TURTLE1",
  key: "-MkeOOUboKdOcC3Sannl",
  title: "hello2"
}

function returnMatchingComment(commentIdKey) {
  let item;
  var i =0;
  for (const post in objectToLoop) {
    console.log(post,i++);
    if (post.comments_text) {
      console.log("APPEND");
      item = Object.values(post.comments_text).find(
        (comment) => comment.key === commentIdKey
      )
    };
    if (item) {
      break;
    }
  }
  return item
}
console.log(
  returnMatchingComment(commentId)
) 

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