ReferenceindexOf

<indexOf>

The index of the first value in a list equal to each target, or 0 where there is none

<indexOf> is a General Operator component that renders, for each value given by the target attribute, the position of the first of its arguments equal to that value. Indices start at 1, and a target that does not occur in the list gives 0. To find where a value belongs in a sorted list rather than whether it is there at all, use <searchSorted>.

Attributes and Properties

Attributes for <indexOf>

Highlighted (3)
asList

boolean. Default value: true. Whether to render the items separated by commas (true) or with no separator (false).

_componentListWithSelectableType. The value, or list of values, to look for.

type

keyword. Component type to interpret bare string children as. Omit it and they are read as what they look like: every piece naming a number makes the list numeric, anything else makes it text. Also decides how target is read, since it has no type of its own.

ValueDescription
numberRead bare strings as numbers, ordered by value.
mathRead bare strings as math expressions, ordered by value.
textRead bare strings as text, ordered alphabetically.
booleanRead bare strings as booleans, ordered with false before true.
Other (5)
avoidScientificNotation

Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

Number of decimal places to display when rendering this number.

displayDigits

Number of significant digits to display when rendering this number.

displaySmallAsZero

Threshold below which numbers are displayed as zero.

padZeros

Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Common to all components (9)
copy

reference. Create an independent copy of another component by reference. Enter a references a $name.

disabled

boolean. Default value: false. Whether this component is disabled and cannot be interacted with.

extend

reference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.

fixed

Default value: false. Whether this component's value is fixed and cannot be modified.

fixLocation

boolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

hide

boolean. Default value: false. Whether to hide this component from the rendered output.

isResponse

boolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.

name

text. The name used to reference this component from elsewhere in the document.

styleNumber

integer. Default value: 1. The style number used to select this component's visual styling from the available style definitions.

Properties for <indexOf name="i">

Common to all components (4)
$i.doenetML

text. The DoenetML source code that produced this component.

$i.hide

boolean. Whether to hide this component from the rendered output.

$i.isResponse

boolean. Whether this component is treated as a response for the purposes of assessment.

$i.styleNumber

integer. The style number used to select this component's visual styling from the available style definitions.

Examples

Example: finding a number

The value 20 occurs twice and the first occurrence is reported. A value that is absent gives 0, which is a position no list entry has.


Example: finding a name

type="text" tells <indexOf> to read the target as text rather than as a number.


Example: looking up several targets at once

There is one result per target, in the order the targets were given. Eve is not on the roster, so her slot is 0 while the others keep their positions.


Example: looking up a parallel list

Finding a position in one list and reading it out of another is the usual reason to want <indexOf> rather than a test for membership.


Attribute Examples

Attribute Example: target

target is read the way bare children are: whitespace separates one target from the next, so target="New York" asks for two of them and neither is on the list. A value with a space in it is written as a <text> of its own and referenced — which is also how the list holding it is built, since <textList>Boston New York</textList> is three cities and not two.