object/array 中的值完全匹配
Exact match on value in object/array
我在计算对象字段中数组中字符串的出现次数时遇到问题。
下面的 XML 有 3 个 'Level3' 项,每个项有 1 个 'TextLine' 字段。
我需要计算变量 'texts' 中的每个文本在负载中出现了多少次。
fun getVasCount(texts) =
sizeOf (Level1.*Level2.*Level3.*TextLine filter (texts contains $))
所以我得到的不是 count:2,而是 count:3,因为 'a text' 是 'This is a text'
的子字符串
var texts = {
"This is a text": "",
"This is another text": ""
}
<?xml version="1.0" encoding="UTF-8"?>
<ns:Level1
xmlns:ns="aaaa:bbbb:cccc:dddd">
<Level2>
<Level3>
<TextLine>This is a text</TextLine>
</Level3>
<Level3>
<TextLine>This is a text</TextLine>
</Level3>
<Level3>
<TextLine>a text</TextLine>
</Level3>
</Level2>
</ns:Level1>
一种方法是计算每个不同文本的出现次数,按值对它们进行分组,并计算每组中有多少元素。
然后用计数修改您的 texts
对象。
%dw 2.0
output application/json
var texts = {
"This is a text": "",
"This is another text": ""
}
var textLines = payload.Level1.*Level2.*Level3.*TextLine default []
var grouped = textLines
groupBy $
mapObject (groupValues, text) -> {(text): sizeOf(groupValues)}
---
texts mapObject {($$): grouped[$$] default 0}
结果将是:
{
"This is a text": 2,
"This is another text": 0
}
注意:不是真正的修改 texts
,因为对象是不可变的,它正在创建一个新对象。
我在计算对象字段中数组中字符串的出现次数时遇到问题。
下面的 XML 有 3 个 'Level3' 项,每个项有 1 个 'TextLine' 字段。
我需要计算变量 'texts' 中的每个文本在负载中出现了多少次。
fun getVasCount(texts) =
sizeOf (Level1.*Level2.*Level3.*TextLine filter (texts contains $))
所以我得到的不是 count:2,而是 count:3,因为 'a text' 是 'This is a text'
的子字符串var texts = {
"This is a text": "",
"This is another text": ""
}
<?xml version="1.0" encoding="UTF-8"?>
<ns:Level1
xmlns:ns="aaaa:bbbb:cccc:dddd">
<Level2>
<Level3>
<TextLine>This is a text</TextLine>
</Level3>
<Level3>
<TextLine>This is a text</TextLine>
</Level3>
<Level3>
<TextLine>a text</TextLine>
</Level3>
</Level2>
</ns:Level1>
一种方法是计算每个不同文本的出现次数,按值对它们进行分组,并计算每组中有多少元素。
然后用计数修改您的 texts
对象。
%dw 2.0
output application/json
var texts = {
"This is a text": "",
"This is another text": ""
}
var textLines = payload.Level1.*Level2.*Level3.*TextLine default []
var grouped = textLines
groupBy $
mapObject (groupValues, text) -> {(text): sizeOf(groupValues)}
---
texts mapObject {($$): grouped[$$] default 0}
结果将是:
{
"This is a text": 2,
"This is another text": 0
}
注意:不是真正的修改 texts
,因为对象是不可变的,它正在创建一个新对象。