【问题标题】:Check if object key exsist in object - javascript检查对象中是否存在对象键 - javascript
【发布时间】:2020-06-18 04:57:30
【问题描述】:

我做了一个循环来检查一个键是否不存在于另一个对象中。一旦此条件为真,它应该停止并重定向到某个 URL。我让循环工作,但我的问题是,只要条件得到满足。它仍然继续循环剩余的项目。这意味着它永远不会停止并创建某种无限循环。我如何确定是否满足条件(如果)。循环停止。

所需资源:

资源: (第一次为空)

循环:

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

【问题讨论】:

  • 简单的Object.keys(yourObject).indexOf("key_you_are_checking_for") !== -1,不需要forEach
  • 您可以使用Array.find() 代替forEach(),但我不明白显示的代码如何创建无限循环。
  • 如果这是一个专用功能(即没有其他事情可以做),那么您可以插入一个 [return] 语句。

标签: javascript loops object foreach


【解决方案1】:

您可以使用some() 数组方法,例如:

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
}

编辑:

根据讨论,您可以像这样更新您的代码以实现所需的行为,因为我们无法从 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 即可在此处跳出循环。

【讨论】:

  • 为什么不直接:Object.keys(resources).indexOf(item) > -1
  • 我是否知道使用此方法缺少哪个资源,我需要知道它,因为我必须将其作为查询字符串发送
  • 您的要求我不清楚。您是想在循环中的资源未找到时停止循环,还是想在找到资源后立即停止循环?
  • 如果requiredResource(linkedin)在resources(例如google、youtube)中丢失。比停止获取 requiredResource.name(例如linkedin)并转到不同的 url。一些魔法将会发生,资源链接被添加到列表中。下次这个人进入这个循环。 requiredResource(linkedin)在资源(google、youtube、linkedi)中,所以你可以继续
【解决方案2】:

你可以试试这样的...

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语句来打破循环,这有帮助吗?

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 2019-11-12
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多