Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查object-javascript中是否存在对象键_Javascript_Loops_Object_Foreach - Fatal编程技术网

检查object-javascript中是否存在对象键

检查object-javascript中是否存在对象键,javascript,loops,object,foreach,Javascript,Loops,Object,Foreach,我做了一个循环来检查钥匙是否存在于另一个物体中。一旦这个条件为真,它应该停止并重定向到某个URL。我让循环工作,但我的问题是,一旦条件满足。它仍然会继续循环查找其余的项目。这意味着它将永远不会停止,并创建某种infite循环。我如何确保条件(if)是否满足。循环停止 所需资源: // For every requiredResource check if it exist in the resources. (right now it is one) requiredResource.f

我做了一个循环来检查钥匙是否存在于另一个物体中。一旦这个条件为真,它应该停止并重定向到某个URL。我让循环工作,但我的问题是,一旦条件满足。它仍然会继续循环查找其余的项目。这意味着它将永远不会停止,并创建某种infite循环。我如何确保条件(if)是否满足。循环停止

所需资源:

// 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`);
        }
    });

资源:(第一次为空)

循环:

// 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`);
        }
    });
您可以使用数组方法进行以下操作:

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
}
编辑:

// 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`);
        }
    });
根据讨论,您可以像这样更新代码以实现所需的行为,因为我们不能从
forEach
循环中中断:

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

只需将
some()
return true
一起使用,就可以打破这里的循环

你可以试试这样的

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

然后使用return语句来打破循环,这样有帮助吗?

只需做一个简单的
Object.keys(你的对象)。indexOf(“你正在检查的键”)!==-1
,无需使用
forEach
。您可以使用
Array.find()
而不是
forEach()
,但我不明白显示的代码是如何创建一个无休止的循环的。如果这是一个专用函数(即它没有其他功能),那么您可以插入一个[return]语句。为什么不使用:
Object.keys(resources).indexOf(项)>-1
?我是否知道使用此方法缺少哪些资源,我需要知道它,因为我必须将其作为查询字符串发送。我不清楚您的要求。是否希望在循环中的资源未找到时停止循环,或者您希望在找到资源后立即停止循环?如果
需要的资源
(linkedin)
资源中缺少(例如google、youtube)。然后停止抓取所需的resource.name(例如linkedin)并转到其他url。会出现一些魔术,资源linkedin会添加到列表中。下次此人进入此循环时。所需资源(linkedin)位于资源(google、youtube、linkedi)中所以你可以继续