简体   繁体   中英

get index of a value from list/array in dataweave

I have an array in dataweave lets say

[value1,value2,value3,value4] 

I would like to get the index of my value in array. Suppose if I want to know at which index position 'value2' is present, it should return 2.

**code I tried:**

%dw 1.0
%output application/xml
%var myArray =[] as :array
---
{
 root: using (myArray = payload.values.*value distinctBy $) {
 (payload.values.*value map{
    position: "needs logic to get position of current value in the myArray created"
  })
}


**input:**
<values>
  <value>value1</value>
  <value>value2</value>
  <value>value3</value>
  <value>value4</value>
  <value>value1</value>
  <value>value3</value>
</values>


expected output:
  <root>
    <position>1<position>
    <position>2<position>
    <position>3<position>
    <position>4<position>
    <position>1<position>
    <position>3<position>
  <root>

WHat about :

%dw 1.0
%output application/xml
%var myArray =[] as :array
---
{
 root: using (myArray = payload.values.*value distinctBy $) {
    (payload.values.*value map (
        position: $$
    )) }
}

This outputs:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <position>0</position>
  <position>1</position>
  <position>2</position>
  <position>3</position>
  <position>4</position>
  <position>5</position>
</root>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM