0

The ask here is, when we use map function over an array, it iterated the # of times the total objects present, we use $$ for iteration count, now after each object iteration, the $$ gets reset to 0, but I want for next iteration the iteration count should move ahead with +1 instead of 0.

I have a request payload as:

[
    {
        "id": 1
    },
    {
        "id": 2
    },
    {
        "id": 3
    },
    {
        "id": 4
    }
]

The final response expected is:

{
  "products": [
    {
      "iterater": 1,
      "child": [
        {
          "childIterater": 2
        },
        {
          "childIterater": 3
        },
        {
          "childIterater": 4
        },
        {
          "childIterater": 5
        }
      ]
    },
    {
      "iterater": 6,
      "child": [
        {
          "childIterater": 7
        },
        {
          "childIterater": 8
        },
        {
          "childIterater": 9
        },
        {
          "childIterater": 10
        }
      ]
    },
    {
      "iterater": 11,
      "child": [
        {
          "childIterater": 12
        },
        {
          "childIterater": 13
        },
        {
          "childIterater": 14
        },
        {
          "childIterater": 15
        }
      ]
    },
    {
      "iterater": 16,
      "child": [
        {
          "childIterater": 17
        },
        {
          "childIterater": 18
        },
        {
          "childIterater": 19
        },
        {
          "childIterater": 20
        }
      ]
    }
  ]
}

products is mapped over request payload and that is why there are 4 objects, inside products, child is again mapped with request payload, so each object again has 4 objects.

1
  • What do you mean by "continuously iterate in multiple arrays"? You only shared a single input array. Are there others? Commented Sep 26, 2023 at 11:35

1 Answer 1

2

I'm assuming that there is only one input that you want to map each element to the same input array to create children. The ids of the input seem to be ignored in the output.

DataWeave is a functional language so you can not just loop and use counters. However in this case the counter can be calculated as a function of the numbers of elements.

%dw 2.0
output application/json
fun mapChilds(a, order)=a map {
    childIterator: order + $$ + 1
}
---
{
    products: payload map {
        iterator: $$ + 1 + ($$*sizeOf(payload)),
        child: (mapChilds(payload, $$ + 1 + ($$*sizeOf(payload))))
    }
}

Output:

{
  "products": [
    {
      "iterator": 1,
      "child": [
        {
          "childIterator": 2
        },
        {
          "childIterator": 3
        },
        {
          "childIterator": 4
        },
        {
          "childIterator": 5
        }
      ]
    },
    {
      "iterator": 6,
      "child": [
        {
          "childIterator": 7
        },
        {
          "childIterator": 8
        },
        {
          "childIterator": 9
        },
        {
          "childIterator": 10
        }
      ]
    },
    {
      "iterator": 11,
      "child": [
        {
          "childIterator": 12
        },
        {
          "childIterator": 13
        },
        {
          "childIterator": 14
        },
        {
          "childIterator": 15
        }
      ]
    },
    {
      "iterator": 16,
      "child": [
        {
          "childIterator": 17
        },
        {
          "childIterator": 18
        },
        {
          "childIterator": 19
        },
        {
          "childIterator": 20
        }
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.