Skip to Content
ReferenceindexOf

<indexOf>

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

<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.
Number display (5)
avoidScientificNotation

boolean. Default value: false. Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

integer. Default value: 2. Number of decimal places to display when rendering this number.

displayDigits

integer. Default value: 3. Number of significant digits to display when rendering this number.

displaySmallAsZero

number. Default value: 1e-14. Threshold below which numbers are displayed as zero.

padZeros

boolean. Default value: false. 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

boolean. 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">

Number display (5)
$i.avoidScientificNotationIn development

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

$i.displayDecimalsIn development

integer. Number of decimal places to display when rendering this number.

$i.displayDigitsIn development

integer. Number of significant digits to display when rendering this number.

$i.displaySmallAsZeroIn development

number. Threshold below which numbers are displayed as zero.

$i.padZerosIn development

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

Common to all components (8)
$i.disabledIn development

boolean. Whether this component is disabled and cannot be interacted with.

$i.doenetML

text. The DoenetML source code that produced this component.

$i.fixedIn development

boolean. Whether this component's value is fixed and cannot be modified.

$i.fixLocationIn development

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

$i.hiddenIn development

boolean. Whether this component is hidden from the rendered output.

$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.


Example: indexing without naming the lookup

The <indexOf> can be written between the index brackets instead of being named and referenced. Both find the same position, so which to use is a matter of how the sentence reads: naming it keeps a long index out of the prose, and is what to do when the same position is wanted more than once.

A property can follow the brackets, as the second paragraph shows, and so can a further index.


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.

Last updated on