Skip to main content
I misread part of the question, so have provided an updated example
Source Link
MrWillihog
  • 2.7k
  • 20
  • 17

I would firstly filter the arrayYou need to do 2 things. Firstly, you're being returned an array and you're only includeinterested in a subset of the itemselements. This is a common pattern that are blocker violationsis solved by a filter, or select in Ruby. Secondly, the condition by which you wish to select these elements also depends on the values of another array, which you need to filter using a different technique. You could attempt it like this:

res = [
  {
    "id": 10008,
    "name": "vpop-fms-inventory-ws-client",
    "msr": [
      {
        "key": "blocker_violations",
        "val": 123,
        "frmt_val": "0"
      }
    ]
  },
  {
    "id": 10008,
    "name": "vpop-fms-inventory-ws-client",
    "msr": [
      {
        "key": "safe",
        "val": 0,
        "frmt_val": "0"
      }
    ]
  }
]

# define a lambda function that we will use later on to filter out the blocker violations
violation = -> (h.select) {|h1| h1['msr']['key']h[:key] == 'blocker_violations' }

You can then map these objects to the val properties:


# Select only those objects who contain any msr with a key of     blocker_violations
violations = res.select {|h1| h1[:msr].any? &violation }

# Which msr value should we take? Here I just take the first.
values = violations.map {|v| v['msr']['val']v[:msr].first[:val] }

You should runThe problem you may have with this code is that msr is an array. So theoretically, you could end up with caution though2 objects in msr, asone that is a blocker violation and one that is not. You have to decide how you handle that. In my example, I include it assumesif it has a single blocker violation through the use of any?. However, you may wish to only include them if all msr objects are blocker violations. You can do this via the all? method.

The second problem you then face is, which value to return? If there are multiple blocker violations in the msr object exists for each result, which mayvalue do you choose? I just took the first one - but this might not be truework for you.

Depending on your requirements, my example might work or you might need to adapt it.

Also, if you've never come across the lambda syntax before, you can read more about it here

I would firstly filter the array to only include the items that are blocker violations:

violations = h.select {|h1| h1['msr']['key'] == 'blocker_violations' }

You can then map these objects to the val properties:

values = violations.map {|v| v['msr']['val'] }

You should run this code with caution though, as it assumes the msr object exists for each result, which may not be true.

You need to do 2 things. Firstly, you're being returned an array and you're only interested in a subset of the elements. This is a common pattern that is solved by a filter, or select in Ruby. Secondly, the condition by which you wish to select these elements also depends on the values of another array, which you need to filter using a different technique. You could attempt it like this:

res = [
  {
    "id": 10008,
    "name": "vpop-fms-inventory-ws-client",
    "msr": [
      {
        "key": "blocker_violations",
        "val": 123,
        "frmt_val": "0"
      }
    ]
  },
  {
    "id": 10008,
    "name": "vpop-fms-inventory-ws-client",
    "msr": [
      {
        "key": "safe",
        "val": 0,
        "frmt_val": "0"
      }
    ]
  }
]

# define a lambda function that we will use later on to filter out the blocker violations
violation = -> (h) { h[:key] == 'blocker_violations' }

# Select only those objects who contain any msr with a key of     blocker_violations
violations = res.select {|h1| h1[:msr].any? &violation }

# Which msr value should we take? Here I just take the first.
values = violations.map {|v| v[:msr].first[:val] }

The problem you may have with this code is that msr is an array. So theoretically, you could end up with 2 objects in msr, one that is a blocker violation and one that is not. You have to decide how you handle that. In my example, I include it if it has a single blocker violation through the use of any?. However, you may wish to only include them if all msr objects are blocker violations. You can do this via the all? method.

The second problem you then face is, which value to return? If there are multiple blocker violations in the msr object, which value do you choose? I just took the first one - but this might not work for you.

Depending on your requirements, my example might work or you might need to adapt it.

Also, if you've never come across the lambda syntax before, you can read more about it here

Source Link
MrWillihog
  • 2.7k
  • 20
  • 17

I would firstly filter the array to only include the items that are blocker violations:

violations = h.select {|h1| h1['msr']['key'] == 'blocker_violations' }

You can then map these objects to the val properties:

values = violations.map {|v| v['msr']['val'] }

You should run this code with caution though, as it assumes the msr object exists for each result, which may not be true.