X Tutup
Skip to content

Latest commit

 

History

History
67 lines (54 loc) · 1.77 KB

File metadata and controls

67 lines (54 loc) · 1.77 KB
layout language permalink command io related_commands
api-command
JavaScript
api/javascript/coerce_to/
coerceTo
sequence
array
value
string
array
object
object
array
binary
string
string
binary
object
object/

Command syntax

{% apibody %} sequence.coerceTo('array') → array value.coerceTo('string') → string string.coerceTo('number') → number array.coerceTo('object') → object sequence.coerceTo('object') → object object.coerceTo('array') → array binary.coerceTo('string') → string string.coerceTo('binary') → binary {% endapibody %}

Description

Convert a value of one type into another.

  • a sequence, selection or object can be coerced to an array
  • a sequence, selection or an array of key-value pairs can be coerced to an object
  • a string can be coerced to a number
  • any datum (single value) can be coerced to to a string
  • a binary object can be coerced to a string and vice-versa

Example: Coerce a stream to an array to store its output in a field. (A stream cannot be stored in a field directly.)

r.table('posts').map(function (post) {
    return post.merge({ comments: r.table('comments').getAll(post('id'), {index: 'postId'}).coerceTo('array')});
}).run(conn, callback)

Example: Coerce an array of key-value pairs into an object.

r.expr([['name', 'Ironman'], ['victories', 2000]]).coerceTo('object').run(conn, callback)

Note: To coerce a list of key-value pairs like ['name', 'Ironman', 'victories', 2000] to an object, use the object command.

Example: Coerce a number to a string.

r.expr(1).coerceTo('string').run(conn, callback)
X Tutup