2

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

4
  • "and if the key within comments_text matches commentId" - Then why Object.values(post.comments_text)? Commented Oct 12, 2021 at 8:01
  • 3
    for..in loops over object keys. Not the values. Keys are strings. Strings don't have a comments_text property. Commented Oct 12, 2021 at 8:03
  • 2
    Are you 100% sure objectToLoop is meant to be an object? Commented Oct 12, 2021 at 8:03
  • 3
    Since objectToLoop is an object, you don't need a loop and can instead use: let item = objectToLoop.comments_text Commented Oct 12, 2021 at 8:06

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

1 Comment

doesn't answer question in OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.