Это старая версия документа!


DataWeave

%dw 2.0
input payload json
output application/java
---
payload
%dw 2.0
output application/dw
---
payload
  • countBy Counts the elements in an array that return true when the matching function is applied to the value of each element.
  • divideBy Breaks up an array into sub-arrays that contain the specified number of elements.
  • drop Drops the first n elements. It returns the original array when n ⇐ 0 and an empty array when n > sizeOf(array).
  • dropWhile Drops elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.
  • every Returns true if every element in the array matches the condition.
  • firstWith Returns the first element that satisfies the condition, or returns null if no element meets the condition.
  • indexOf Returns the index of the first occurrence of an element within the array. If the value is not found, the function returns -1.
  • indexWhere Returns the index of the first occurrence of an element that matches a condition within the array. If no element matches the condition, the function returns -1.
  • join Joins two arrays of objects by a given ID criteria.
  • leftJoin Joins two arrays of objects by a given ID criteria.
  • outerJoin Joins two array of objects by a given ID criteria.
  • partition Separates the array into the elements that satisfy the condition from those that do not.
  • slice Selects the interval of elements that satisfy the condition: from ⇐ indexOf(array) < until
  • some Returns true if at least one element in the array matches the specified condition.
  • splitAt Splits an array into two at a given position.
  • splitWhere Splits an array into two at the first position where the condition is met.
  • sumBy Returns the sum of the values of the elements in an array.
  • take Selects the first n elements. It returns an empty array when n ⇐ 0 and the original array when n > sizeOf(array).
  • takeWhile Selects elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.

reduce

Функция уменьшения используется для выполнения любых вычислений во время итерации массива, поэтому результат вычислений не теряется во время итерации. Для каждого элемента входного массива по порядку сокращение применяет лямбда-выражение (функцию) сокращения, а затем заменяет аккумулятор новым результатом. Лямбда-выражение может использовать как текущий элемент входного массива, так и текущее значение аккумулятора.

%dw 2.0
output json
---
payload reduce (n, total) -> total + n
%dw 2.0
output application/json
---
[2, 3] reduce ($ + $$)

Here

    [2,3] – is the input array
    acc -> $$ will take the 1st item value = 2 (as acc is not initialized)
    $ will take the 2nd item value = 3 (as acc is not initialized)
    Loop count = no of item in array minus 1 (as acc is not initialized) = 2 – 1 = 1
        Acc = ($=3 + $$=2) = 5

So result will be 5
%dw 2.0
output application/json
---
[2,3] reduce ((item, acc = 4) -> acc + item)

Here

    [2,3] – is the input array
    acc will take the initialized value = 4
    item will take 1st item value = 2 (as acc is initialized)
    Loop count = no of item in array (as acc is initialized) = 2
        Acc = acc + item -> 4 + 2 -> 6
        Acc = acc + item -> 6 + 3 -> 9

So result will be 9
  • divideBy Breaks up an object into sub-objects that contain the specified number of key-value pairs.
  • entrySet Returns an array of key-value pairs that describe the key, value, and any attributes in the input object.
  • everyEntry Returns true if every entry in the object matches the condition.
  • keySet Returns an array of key names from an object.
  • mergeWith Appends any key-value pairs from a source object to a target object.
  • nameSet Returns an array of keys from an object.
  • someEntry Returns true if at least one entry in the object matches the specified condition.
  • takeWhile Selects key-value pairs from the object while the condition is met.
  • valueSet Returns an array of the values from key-value pairs in an object.

Ссылки