1

I made a loop to check whether a key doesn't exsist in another object. As soon as this condition is true it should stop and redirect to a certain URL. I got the loop working but my issue is that as soon as the condition is met. It still continues to loop for the remaining items. Meaning it will never stop and creates some kind of infite loop. How can i make sure that if the condition (if) is met. the loop stops.

requiredResource:

enter image description here

resources: (first time it is empty)

enter image description here

Loop:

// For every requiredResource check if it exist in the resources. (right now it is one)
    requiredResource.forEach((item: any) => {
        // Check if there are resources, if not go get them
        if(resources.length !== 0){
            // Resources are filled with 2 examples
            Object.keys(resources).forEach(value => {

                //if the required is not in there, go get this resource and stop the loop
                if(value !== item.name){

                    // Go get the specific resource that is missing
                    window.location.assign(`getspecificresource.com`);

                } else {

                    // Key from resource is matching the required key. you can continue
                    //continue
                }
            });
        } else {
            // get resources
            window.location.assign(`getalistwithresources.com`);
        }
    });
3
  • Just do a simple Object.keys(yourObject).indexOf("key_you_are_checking_for") !== -1, no need for forEach. Commented Mar 5, 2020 at 14:24
  • You can use Array.find() instead of forEach(), but I don't understand how the code shown creates an endless loop. Commented Mar 5, 2020 at 14:26
  • If this is a dedicated function (i.e. nothing else for it to do) then you can just chuck in a [return] statement. Commented Mar 5, 2020 at 14:31

2 Answers 2

2

You can use some() array method for this like:

const found = requiredResource.some(({name}) => Object.keys(resources).indexOf(name) > -1)
if (!found) {
   window.location.assign(`getspecificresource.com`);
} else {
   // Key from resource is matching the required key. you can continue
   //continue
}

EDIT:

Based on the discussion, you can updated your code like this to achieve the required behaviour as we can't break from a forEach loop:

requiredResource.some((item) => {
   // Check if there are resources, if not go get them
   if (resources.length !== 0) {
      // Resources are filled with 2 examples
      Object.keys(resources).some(value => {

         //if the required is not in there, go get this resource and stop the loop
         if (value !== item.name) {

            // Go get the specific resource that is missing
            window.location.assign(`getspecificresource.com`);
            return true;
         } else {

            // Key from resource is matching the required key. you can continue
            //continue            
            return false;
         }
      });
      return false;
   } else {
      // get resources
      window.location.assign(`getalistwithresources.com`);
      return true;
   }
});

Just using the some() with return true to break out of the loop here.

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

4 Comments

Why not just: Object.keys(resources).indexOf(item) > -1?
Do i know with this method which resource is missing, i need to know it because i have to send it as query string
Your requirement is not clear to me. Do you want to stop the loop when the resource in the loop is not found or you want to stop the loop as soon as a resource is found?
if the requiredResource (linkedin) is missing in the resources (e.g. google, youtube). Than stop grab the requiredResource.name (e.g. linkedin) and go to a different url. Some magic will hapen and the resource linkedin is added to the list. Next time the person enters this loop. requiredResource (linkedin) is in the resources (google, youtube, linkedi) so you can continue
0

You could try something like this...

let obj = {
  'a': 1,
  'b': 2,
  'c': 3
}
console.log(obj.a)
  Object.keys(obj).some(x=>{
    console.log(obj[x])
   return obj[x]==2
  })

And use the return statement to break the loop, does that help?

Comments

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.