Dataweave 2.2 takeWhile 和 filter 有什么区别?
Dataweave 2.2 What's the difference between takeWhile and filter?
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,4,3]
---
arr filter $ <= 2
和
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,4,3]
---
arr takeWhile $ <= 2
它们都给出了相同的结果。有区别吗?
嗨,戴尔,有一个区别 takeWhile 将停止获取第一个不满足条件的元素,这不是过滤器的情况,所以对于这个例子 [0,2,4,3 ,1]
使用 TakeWhile
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr takeWhile $ <= 2
Returns:
[
0,
2
]
有过滤器
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr filter $ <= 2
Returns:
[
0,
2,
1
]
takeWhile :在满足条件时从数组中选择元素,但在到达不满足条件的元素时停止 selection 过程。
脚本
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr takeWhile $ <= 2
输出
[
0,
2
]
filter :对select所有满足条件的元素,使用函数。
脚本
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr filter $ <= 2
输出
[
0,
2,
1
]
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,4,3]
---
arr filter $ <= 2
和
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,4,3]
---
arr takeWhile $ <= 2
它们都给出了相同的结果。有区别吗?
嗨,戴尔,有一个区别 takeWhile 将停止获取第一个不满足条件的元素,这不是过滤器的情况,所以对于这个例子 [0,2,4,3 ,1]
使用 TakeWhile
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr takeWhile $ <= 2
Returns:
[
0,
2
]
有过滤器
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr filter $ <= 2
Returns:
[
0,
2,
1
]
takeWhile :在满足条件时从数组中选择元素,但在到达不满足条件的元素时停止 selection 过程。
脚本
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr takeWhile $ <= 2
输出
[
0,
2
]
filter :对select所有满足条件的元素,使用函数。 脚本
%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,2,4,3,1]
---
arr filter $ <= 2
输出
[
0,
2,
1
]