How check if shape vector contains a shape with a certain value

Viewed 308
shape x {
   ?'a' => ?string,
   ?'b' => ?string,

}

I have an array of shape x, And I am trying to see if any of the shapes have a value of 'hello' in field 'a'. How can i do so using built in functions?

1 Answers
function has_hello(vec<shape(?'a' => string, ?'b' => string)> $items): bool {
  foreach ($items as $item) {
    if (Shapes::idx($item, 'a') === 'hello') {
      return true;
    }
  }
  return false;
}

You can use Shapes::idx to get the value of an optional field. If you're looking for helper functions to iterate on arrays, check out the C\ namespace.

Related