.map()
Transform each element in an array.
bash
echo '[1, 2, 3]' | 1ls '.map(x => x * 2)'
# Output: [2, 4, 6]All standard JavaScript array methods are supported.
Transform each element in an array.
echo '[1, 2, 3]' | 1ls '.map(x => x * 2)'
# Output: [2, 4, 6]Keep elements that match a condition.
echo '[1, 2, 3, 4, 5]' | 1ls '.filter(x => x > 2)'
# Output: [3, 4, 5]Reduce an array to a single value.
echo '[1, 2, 3, 4]' | 1ls '.reduce((sum, x) => sum + x, 0)'
# Output: 10Find the first element matching a condition.
echo '[{"id": 1}, {"id": 2}, {"id": 3}]' | 1ls '.find(x => x.id === 2)'
# Output: {"id": 2}Find the index of the first matching element.
echo '[10, 20, 30]' | 1ls '.findIndex(x => x === 20)'
# Output: 1Check if any element matches a condition.
echo '[1, 2, 3]' | 1ls '.some(x => x > 2)'
# Output: trueCheck if all elements match a condition.
echo '[2, 4, 6]' | 1ls '.every(x => x % 2 === 0)'
# Output: trueSort array elements.
echo '[3, 1, 2]' | 1ls '.sort((a, b) => a - b)'
# Output: [1, 2, 3]Reverse the array order.
echo '[1, 2, 3]' | 1ls '.reverse()'
# Output: [3, 2, 1]Extract a portion of an array.
echo '[1, 2, 3, 4, 5]' | 1ls '.slice(1, 4)'
# Output: [2, 3, 4]Flatten nested arrays.
echo '[[1, 2], [3, 4]]' | 1ls '.flat()'
# Output: [1, 2, 3, 4]Map then flatten the result.
echo '[1, 2]' | 1ls '.flatMap(x => [x, x * 2])'
# Output: [1, 2, 2, 4]Join array elements into a string.
echo '["a", "b", "c"]' | 1ls '.join("-")'
# Output: "a-b-c"