method
RedisClient.lpos
lpos(
...options: string | number[]
): Promise<null | number | number[]>;
Find the position(s) of an element in a list
Returns the index of matching elements inside a Redis list. By default, returns the index of the first match. Use RANK to find the nth occurrence, COUNT to get multiple positions, and MAXLEN to limit the search.
@param key
The list key
@param element
The element to search for
@param options
Optional arguments: "RANK", rank, "COUNT", num, "MAXLEN", len
@returns
Promise that resolves with the index (number), an array of indices (number[]), or null if element is not found. Returns array when COUNT option is used.
await redis.lpush("mylist", "a", "b", "c", "b", "d");
const pos1 = await redis.lpos("mylist", "b");
// pos1: 1 (first occurrence of "b")
const pos2 = await redis.lpos("mylist", "b", "RANK", 2);
// pos2: 3 (second occurrence of "b")
const positions = await redis.lpos("mylist", "b", "COUNT", 0);
// positions: [1, 3] (all occurrences of "b")
const pos3 = await redis.lpos("mylist", "x");
// pos3: null (element not found)Referenced types
type KeyLike = string | ArrayBufferView | Blob