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

151
Views
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 answers
Answer question

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 Report

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 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!