.head / hd()
Get the first element of an array.
echo '[1, 2, 3, 4, 5]' | 1ls 'head()'
# Output: 1jq-compatible builtin functions for data transformation. All builtins have shorthand aliases.
Functions for working with arrays.
Get the first element of an array.
echo '[1, 2, 3, 4, 5]' | 1ls 'head()'
# Output: 1Get the last element of an array.
echo '[1, 2, 3, 4, 5]' | 1ls 'last()'
# Output: 5Get all elements except the first.
echo '[1, 2, 3, 4, 5]' | 1ls 'tail()'
# Output: [2, 3, 4, 5]Take the first n elements.
echo '[1, 2, 3, 4, 5]' | 1ls 'take(3)'
# Output: [1, 2, 3]Drop the first n elements.
echo '[1, 2, 3, 4, 5]' | 1ls 'drop(2)'
# Output: [3, 4, 5]Remove duplicate values.
echo '[1, 2, 2, 3, 3, 3]' | 1ls 'uniq()'
# Output: [1, 2, 3]Flatten nested arrays recursively.
echo '[[1, 2], [3, [4, 5]]]' | 1ls 'flatten()'
# Output: [1, 2, 3, 4, 5]Reverse an array.
echo '[1, 2, 3]' | 1ls 'rev()'
# Output: [3, 2, 1]Split array into chunks of size n.
echo '[1, 2, 3, 4, 5]' | 1ls 'chunk(2)'
# Output: [[1, 2], [3, 4], [5]]Remove falsy values (null, undefined, false, 0, "").
echo '[0, 1, false, 2, "", 3, null]' | 1ls 'compact()'
# Output: [1, 2, 3]Get element at index n (supports negative indexes).
echo '[10, 20, 30, 40]' | 1ls 'nth(-1)'
# Output: 40Sum numbers, concatenate arrays, or join strings.
echo '[1, 2, 3, 4]' | 1ls 'add()'
# Output: 10Functions for working with objects.
Get object keys.
echo '{"a": 1, "b": 2}' | 1ls 'keys()'
# Output: ["a", "b"]Get object values.
echo '{"a": 1, "b": 2}' | 1ls 'vals()'
# Output: [1, 2]Pick specific keys from an object.
echo '{"a": 1, "b": 2, "c": 3}' | 1ls 'pick("a", "c")'
# Output: {"a": 1, "c": 3}Omit specific keys from an object.
echo '{"a": 1, "b": 2, "c": 3}' | 1ls 'omit("b")'
# Output: {"a": 1, "c": 3}Shallow merge objects.
echo '{"a": 1}' | 1ls 'merge({"b": 2})'
# Output: {"a": 1, "b": 2}Deep merge objects recursively.
echo '{"a": {"x": 1}}' | 1ls 'deepMerge({"a": {"y": 2}})'
# Output: {"a": {"x": 1, "y": 2}}Convert array of pairs to object.
echo '[["a", 1], ["b", 2]]' | 1ls 'fromPairs()'
# Output: {"a": 1, "b": 2}Convert object to array of pairs.
echo '{"a": 1, "b": 2}' | 1ls 'toPairs()'
# Output: [["a", 1], ["b", 2]]Check if object has a key.
echo '{"name": "test"}' | 1ls 'has("name")'
# Output: trueExtract a property from each object in an array.
echo '[{"name": "a"}, {"name": "b"}]' | 1ls 'pluck("name")'
# Output: ["a", "b"]Functions for grouping and sorting data.
Group array elements by a key function.
echo '[{"type": "a", "v": 1}, {"type": "b", "v": 2}, {"type": "a", "v": 3}]' | 1ls 'groupBy(x => x.type)'
# Output: {"a": [...], "b": [...]}Sort array by a key function.
echo '[{"age": 30}, {"age": 20}, {"age": 25}]' | 1ls 'sortBy(x => x.age)'
# Output: [{"age": 20}, {"age": 25}, {"age": 30}]Mathematical and aggregation functions.
Sum of numbers in an array.
echo '[1, 2, 3, 4, 5]' | 1ls 'sum()'
# Output: 15Average of numbers in an array.
echo '[1, 2, 3, 4, 5]' | 1ls 'mean()'
# Output: 3Minimum value in an array.
echo '[5, 2, 8, 1, 9]' | 1ls 'min()'
# Output: 1Maximum value in an array.
echo '[5, 2, 8, 1, 9]' | 1ls 'max()'
# Output: 9Floor a number.
echo '3.7' | 1ls 'floor()'
# Output: 3Ceiling of a number.
echo '3.2' | 1ls 'ceil()'
# Output: 4Round a number.
echo '3.5' | 1ls 'round()'
# Output: 4Absolute value.
echo '-5' | 1ls 'abs()'
# Output: 5Functions for working with strings.
Split a string by separator.
echo '"a,b,c"' | 1ls 'split(",")'
# Output: ["a", "b", "c"]Join array elements with separator.
echo '["a", "b", "c"]' | 1ls 'join("-")'
# Output: "a-b-c"Check if string starts with prefix.
echo '"hello world"' | 1ls 'startswith("hello")'
# Output: trueCheck if string ends with suffix.
echo '"hello world"' | 1ls 'endswith("world")'
# Output: trueRemove prefix from string.
echo '"hello world"' | 1ls 'ltrimstr("hello ")'
# Output: "world"Remove suffix from string.
echo '"hello world"' | 1ls 'rtrimstr(" world")'
# Output: "hello"Convert value to string.
echo '42' | 1ls 'tostring()'
# Output: "42"Convert string to number.
echo '"42"' | 1ls 'tonumber()'
# Output: 42Functions for inspecting values.
Get the type of a value.
echo '{"a": 1}' | 1ls 'type()'
# Output: "object"Get length of array, string, or object.
echo '"hello"' | 1ls 'len()'
# Output: 5Count items (alias for len).
echo '[1, 2, 3]' | 1ls 'count()'
# Output: 3Check if value is empty.
echo '[]' | 1ls 'isEmpty()'
# Output: trueCheck if value is null or undefined.
echo 'null' | 1ls 'isNil()'
# Output: trueCheck if value contains another (deep comparison).
echo '[1, 2, 3]' | 1ls 'contains([2])'
# Output: trueFunctions for navigating nested structures.
Get all paths in a structure.
echo '{"a": {"b": 1}}' | 1ls 'path()'
# Output: [[], ["a"], ["a", "b"]]Get value at a path.
echo '{"a": {"b": 1}}' | 1ls 'getpath(["a", "b"])'
# Output: 1Set value at a path.
echo '{"a": {"b": 1}}' | 1ls 'setpath(["a", "b"], 99)'
# Output: {"a": {"b": 99}}Recursively collect all values.
echo '{"a": [1, {"b": 2}]}' | 1ls 'recurse()'
# Output: [..all nested values..]Functions for control flow and composition.
Filter by predicate (jq-style).
echo '[1, 2, 3, 4]' | 1ls '.map(x => select(x > 2))'
# Output: [3, 4]Return nothing (useful in select).
echo '1' | 1ls 'empty()'
# Output: (no output)Throw an error with message.
echo '1' | 1ls 'error("bad value")'
# Output: Error: bad valueOutput debug information.
echo '{"x": 1}' | 1ls 'debug()'
# Output: (logs value, returns input)Boolean negation.
echo 'true' | 1ls 'not()'
# Output: falseGenerate a range of numbers.
echo 'null' | 1ls 'range(5)'
# Output: [0, 1, 2, 3, 4]Functions for composing operations.
Apply expressions left-to-right.
echo '[1, 2, 3]' | 1ls 'pipe(.map(x => x * 2), .filter(x => x > 2))'
# Output: [4, 6]Apply expressions right-to-left.
echo '[1, 2, 3]' | 1ls 'compose(.filter(x => x > 1), .map(x => x * 2))'
# Output: [4, 6]Identity function (returns input unchanged).
echo '{"a": 1}' | 1ls 'id()'
# Output: {"a": 1}