Skip to main content
Module

x/denops_std/function/mod.ts>indexof

📚 Standard module for denops.vim
Go to Latest
function indexof
import { indexof } from "https://deno.land/x/denops_std@v6.4.0/function/mod.ts";

Returns the index of an item in {object} where {expr} is v:true. {object} must be a List or a Blob.

If {object} is a List, evaluate {expr} for each item in the List until the expression is v:true and return the index of this item.

If {object} is a Blob evaluate {expr} for each byte in the Blob until the expression is v:true and return the index of this byte.

{expr} must be a string or Funcref.

If {expr} is a string: If {object} is a List, inside {expr} v:key has the index of the current List item and v:val has the value of the item. If {object} is a Blob, inside {expr} v:key has the index of the current byte and v:val has the byte value.

If {expr} is a Funcref it must take two arguments: 1. the key or the index of the current item. 2. the value of the current item. The function must return TRUE if the item is found and the search should stop.

The optional argument {opts} is a Dict and supports the following items: startidx start evaluating {expr} at the item with this index; may be negative for an item relative to the end Returns -1 when {expr} evaluates to v:false for all the items. Example:

:let l = [#{n: 10}, #{n: 20}, #{n: 30}]
:echo indexof(l, "v:val.n == 20")
:echo indexof(l, {i, v -> v.n == 30})
:echo indexof(l, "v:val.n == 20", #{startidx: 1})

Can also be used as a method:

mylist->indexof(expr)

Parameters

denops: Denops
object: unknown
expr: unknown
optional
opts: unknown

Returns

Promise<number>