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