Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 967 Bytes

nth.md

File metadata and controls

45 lines (35 loc) · 967 Bytes
layout api-command
language JavaScript
permalink api/javascript/nth/
command nth
io
sequence
object
related_commands
skip limit () (bracket) slice orderBy
skip/
limit/
bracket/
slice/
order_by/

Command syntax

{% apibody %} sequence.nth(index) → object selection.nth(index) → selection<object> {% endapibody %}

Description

Get the nth element of a sequence, counting from zero. If the argument is negative, count from the last element.

Example: Select the second element in the array.

r.expr([1,2,3]).nth(1).run(conn, callback)
r.expr([1,2,3])(1).run(conn, callback)

Example: Select the bronze medalist from the competitors.

r.table('players').orderBy({index: r.desc('score')}).nth(2).run(conn, callback)

Example: Select the last place competitor.

r.table('players').orderBy({index: r.desc('score')}).nth(-1).run(conn, callback)