'How to make containing function to Stop
I'm new to programming.
I don't why the function is repeating twice.
I assume this is happening because return statement is still making for (let post of posts) start again/ run again. I want the containing function to stop as soon as
return is present inside child function.
Can someone help me understand why it is happening and how can I stop this looping.
const getPosts = () => {
console.log('current location', window.location.href);
for (let post of posts) {
console.log('.....for loop starts here');
switch (post.matchType) {
case 'contains':
console.log('contains');
if (
currentUrl
.toLowerCase()
.toLowerCase()
.includes(post.url.toLowerCase())
) {
for (let item of workingArray)
if (item === post._id) {
console.log(
'.....................oops item already seen'
);
setVisibility(false);
return;
}
setVisibility(true);
setActivePosts(post);
return post;
}
break;
case 'equals':
console.log('equals');
if (currentUrl.toLowerCase() === post.url.toLowerCase()) {
for (let item of workingArray)
if (item === post._id) {
console.log(
'.....................oops item already seen'
);
setVisibility(false);
return;
}
setVisibility(true);
setActivePosts(post);
return post;
}
break;
}
current location http://localhost:8080/
.....for loop starts here
equals
current location http://localhost:8080/
.....for loop starts here
equals
I'm calling getPosts() in the render like this ...
return (
<Simplecomponent
posts={getPosts()}
/>
</div>
</div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
