【问题标题】:How to get through each properties and test the value?如何通过每个属性并测试值?
【发布时间】:2017-05-31 07:32:44
【问题描述】:

我有一个对象,我需要遍历每个theCtns.services.depends_on 并测试该属性的每个值。如果该值等于我的目标值,我想将其删除。

var theCtns = {
  "version": "2",
  "networks": {
    "default": {
      "ipam": {
        "config": [
          {
            "subnet": "0.0.0.0/26",
            "gateway": "0.0.0.0"
          }
        ]
      }
    }
  },
  "services": {
    "serviceOne": {
      "image": "img1",
      "container_name": "serviceOneName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceTwo",
        "serviceX"
      ]
    },
    "serviceTwo": {
      "image": "img2",
      "container_name": "serviceTwoName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceX"
      ]
    }
  }
}


for (var i= theCtns.services.length-1; i>=0; i--){
 for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
    if (theCtns.services[i].depends_on[j] === "serviceTwo") {
        theCtns.services[i].depends_on.splice(j, 1);
    }
  }
//theCtns.services[i].depends_on = theCtns.services[i].depends_on.filter(x=>x!=="serviceTwo")
}


console.log(theCtns)

实际上这个对象可以有超过 8 个服务,但 2 个足以解决我的问题

所以我的循环应该进入each service 然后each depends_on 并测试该值,但看起来我的循环在某处是错误的

[编辑] 更正了lenght 错字

【问题讨论】:

  • 可以加个jsfiddle吗?
  • @Abhidev 我已经提供了一个 sn-p 但如果你愿意我可以做一个小提琴jsfiddle.net/ysjo9cm4/3 但console.log 没有出现任何内容
  • @Rajesh 它没有任何改变

标签: javascript loops object


【解决方案1】:

问题是theCtns.services 是一个对象,您正尝试使用增量循环来像数组一样迭代它。您需要使用 for...in 循环来迭代对象的键。

var theCtns = {
  "version": "2",
  "networks": {
    "default": {
      "ipam": {
        "config": [
          {
            "subnet": "0.0.0.0/26",
            "gateway": "0.0.0.0"
          }
        ]
      }
    }
  },
  "services": {
    "serviceOne": {
      "image": "img1",
      "container_name": "serviceOneName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceTwo",
        "serviceX"
      ]
    },
    "serviceTwo": {
      "image": "img2",
      "container_name": "serviceTwoName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceX"
      ]
    }
  }
}


for (var i in theCtns.services){
  if(theCtns.services.hasOwnProperty(i)){
      for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
        if (theCtns.services[i].depends_on[j] === "serviceTwo") {
            theCtns.services[i].depends_on.splice(j, 1);
        }
      }
  }
}


console.log(theCtns)

【讨论】:

  • 令人印象深刻,好的,我看到了我的错误。谢谢你的好意见
【解决方案2】:

theCtns.services 不是 Array 并且您正试图像它一样迭代它。它实际上是一个Object。 Object 没有长度,这就是你的循环出错的原因。 theCtns.services[i] 也是错误的,因为它指的是不存在的值,例如 theCtns.services[0]。 我更改了您的代码,以便 i 从对象的实际键中获取其值。

var theCtns = {
  "version": "2",
  "networks": {
    "default": {
      "ipam": {
        "config": [
          {
            "subnet": "0.0.0.0/26",
            "gateway": "0.0.0.0"
          }
        ]
      }
    }
  },
  "services": {
    "serviceOne": {
      "image": "img1",
      "container_name": "serviceOneName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceTwo",
        "serviceX"
      ]
    },
    "serviceTwo": {
      "image": "img2",
      "container_name": "serviceTwoName",
      "volumes": [
        "x:x"
      ],
      "environment": [
        "SOMETHING=bla",
        "SOMETHINGELSE=bli"
      ],
      "depends_on": [
        "serviceX"
      ]
    }
  }
}


for (var i in theCtns.services){
  console.log(i);
  for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
    if (theCtns.services[i].depends_on[j] === "serviceTwo") {
        theCtns.services[i].depends_on.splice(j, 1);
    }
  }
}


console.log(theCtns)

【讨论】:

  • 它与@MrCode 相同,但也感谢您的输入并向我展示我的错误
【解决方案3】:

查看修改后的代码,因为不是数组而是对象,需要使用for...var。此外,您可以直接使用数组方法在depends_on 字段中查找目标值。

var theCtns = {
      "version": "2",
      "networks": {
        "default": {
          "ipam": {
            "config": [
              {
                "subnet": "0.0.0.0/26",
                "gateway": "0.0.0.0"
              }
            ]
          }
        }
      },
      "services": {
        "serviceOne": {
          "image": "img1",
          "container_name": "serviceOneName",
          "volumes": [
            "x:x"
          ],
          "environment": [
            "SOMETHING=bla",
            "SOMETHINGELSE=bli"
          ],
          "depends_on": [
            "serviceTwo",
            "serviceX"
          ]
        },
        "serviceTwo": {
          "image": "img2",
          "container_name": "serviceTwoName",
          "volumes": [
            "x:x"
          ],
          "environment": [
            "SOMETHING=bla",
            "SOMETHINGELSE=bli"
          ],
          "depends_on": [
            "serviceX"
          ]
        }
      }
    }

    for (var idx in theCtns.services) {
        var deps_idx = 
        theCtns.services[idx].depends_on.indexOf('serviceTwo');
        if (deps_idx) {
            theCtns.services[idx].depends_on.splice(idx, 1);
        }
    }
    console.log(theCtns.services);

【讨论】:

  • 我使用了@MrCode 的解决方案,但感谢您花时间解决我的问题
猜你喜欢
  • 2016-04-03
  • 2023-04-02
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多