A rundown of the most used lodash functions in Peaka
In JEXL, we can use all lodash functions. This article provides a list of the most frequently used among them.
If you want to see more lodash functions, check out this article.
Checks if value is classified as an Array object.
{{ isArray([1, 2, 3]) }}
// => true
{{ isArray('text') }}
// => false
Creates a new array concatenating array with any additional arrays and/or values.
{{ concat([1], 2, [3], [[4]]) }}
// => [1, 2, 3, [4]]
Gets the index where the first occurrence of value is found in array. If fromIndex is negative, it's used as the offset from the end of array.
{{ _.indexOf([1, 2, 1, 2], 2) }}
// => 1
// Search from the `fromIndex`.
{{ indexOf([1, 2, 1, 2], 2, 2) }}
// => 3
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
{{ reverse([1, 2, 3]) }}
// => [3, 2, 1]
Creates a slice of array from start up to, but not including, the end.
{{ slice([1,2,3,4,5],2,4)}}
// [3,4]
Creates an array excluding all given values.
{{ without([2, 1, 2, 3], 1, 2) }}
// => [3]
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
{{
filter(
[
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false },
],
{ age: 36, active: true }
);
}}
// => ['barney']
{{
filter(
[
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false },
],
["active", false]
);
}}
// => ['fred']
Compares value
and other
, return true or false according to they equals each other or not.
{{ eq( 1, 1 ) }}
// => true
{{ eq( 1, 'some text' ) }}
// => false
Computes the sum of the values in array.
{{ sum([4, 2, 8, 6]) }}
// => 20
### max(array)
Computes the maximum value of array. If array is empty or falsey, undefined is returned.
{{ max([4, 2, 8, 6]) }}
// => 8
{{ max([]) }}
// => undefined
Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
{{
get({ a: [{ b: { c: 3 } }] }), "a[0].b.c";
}}
// => 3
{{
get({ a: [{ b: { c: 3 } }] }),['a', '0', 'b', 'c'];
}}
// => 3
Checks if path is a direct or inherited property of object.
{{ hasIn({ a: { b:2 }}, 'a') }}
// => true
{{ hasIn({ a: { b:2 }}, 'a.b') }}
// => true
{{ hasIn({ a: { b:2 }}, ['a', 'b']) }}
// => true
{{ hasIn({ a: { b:2 }}, 'b') }}
// => false