Loading TOC...

fn.insertBefore

fn.insertBefore(
   target as Sequence,
   position as Number,
   inserts as Sequence
) as Sequence

Summary

Returns a new sequence constructed from the value of $target with the value of $inserts inserted at the position specified by the value of $position. (The value of $target is not affected by the sequence construction.)

If $target is the empty sequence, $inserts is returned. If $inserts is the empty sequence, $target is returned.

The value returned by the function consists of all items of $target whose index is less than $position, followed by all items of $inserts, followed by the remaining elements of $target, in that sequence.

If $position is less than one (1), the first position, the effective value of $position is one (1). If $position is greater than the number of items in $target, then the effective value of $position is equal to the number of items in $target plus 1.

For detailed semantics see, Section 7.2.15 The fn:insert-before function[FS].

Parameters
target The sequence of items into which new items will be inserted. If you pass in a single value, it is treated as a Sequence with that single item; therefore, if you pass in an array, the array is treated as a single value (not as one value for each item in the array). If you mean to pass in the values of each item in the array, then you can call xdmp.arrayValues on the array.
position The position in the target sequence at which the new items will be added.
inserts The items to insert into the target sequence. If you pass in a single value, it is treated as a Sequence with that single item; therefore, if you pass in an array, the array is treated as a single value (not as one value for each item in the array). If you mean to pass in the values of each item in the array, then you can call xdmp.arrayValues on the array.

Example

const x = xdmp.arrayValues(["a", "b", "c"]);
fn.insertBefore(x, 0, "z");

=> ("z", "a", "b", "c")

const x = xdmp.arrayValues(["a", "b", "c"]);
fn.insertBefore(x, 1, "z");
=> ("z", "a", "b", "c")

const x = xdmp.arrayValues(["a", "b", "c"]);
fn.insertBefore(x, 2, "z");
=> ("a", "z", "b", "c")

const x = xdmp.arrayValues(["a", "b", "c"]);
fn.insertBefore(x, 3, "z");
=> ("a", "b", "z", "c")

const x = xdmp.arrayValues(["a", "b", "c"]);
fn.insertBefore(x, 4, "z");
=> ("a", "b", "c", "z")

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.