1

我在 dataweave 中有一个数组可以说

[value1,value2,value3,value4] 

我想在数组中获取我的值的索引。假设如果我想知道 'value2' 存在于哪个索引位置,它应该返回 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>
4

1 回答 1

1

关于什么 :

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

这输出:

<?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>
于 2016-02-04T16:05:58.450 回答