skip to Main Content

I have a parameter that is an array of objects. One of the elements in the object (‘peers’) is another array of objects. I want to dynamically extract the peers array and union / concat them and keeping the ‘vnetid’ as a reference.

param vnets array = [
  {
    vnetid: 0
    name: '0-test-test'
    addressPrefix: '10.10.74.0/22'
    peers: [
      {
        name: '0-to-1'
        peerToId: 1
      }
    ]
    subnets: [
      {
        name: 'GatewaySubnet'
        addressPrefix: '10.10.74.0/25'
      }
      {
        name: 'AzureFirewallSubnet'
        addressPrefix: '10.10.74.128/25'
      }
    ]
  }
  {
    vnetid: 1
    name: '1-test-test'
    addressPrefix: '10.10.68.0/22'
    peers: [
      {
        name: '1-to-0'
        peerToId: 0
      }
    ]
    subnets: [
      {
        name: 'subnet1'
        addressPrefix: '10.10.68.0/25'
      }
      {
        name: 'subnet2'
        addressPrefix: '10.10.69.0/24'
      }
    ]
  }
]

Desired new array:

param peers array = [
  {
    vnetid: 0
    name: '0-to-1'
    peerToId: 1
  }
  {
    vnetid: 1
    name: '1-to-0'
    peerToId: 0
  }
]

I can not find a solution. I have tried with concat, union, items and basically all the array functions. Without any luck.

2

Answers


  1. Chosen as BEST ANSWER

    Nice. Thanks @Thomas. Works perfectly.


  2. You could use the map and flatten lambda functions as described here:

    var peers = flatten(
      // Iterate through the vnets
      map(vnets, vnet => 
         // Iterate through the peers and add vnetid to the peer object  
         map(vnet.peers, peer => 
           union({ vnetid: vnet.vnetid }, peer)
        )
      )
    )
    
    output peers array = peers
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search