json.indexOf: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: {{MacroFunction |name=json.indexOf |version=1.3b53 |description= Returns the index of the first occurrence of a value in the JSON Array. If the value does not exist in the [[JSON Array...)
 
m (Minor formatting changes, corrected examples, added see also.)
Line 3: Line 3:
|version=1.3b53
|version=1.3b53
|description=
|description=
Returns the index of the first occurrence of a value in the [[JSON Array]]. If the value does not
Returns the index of the first occurrence of a value in the [[JSON Array]]. If the value does not exist in the [[JSON Array]] then {{code|-1}} is returned. All [[JSON Array]] indexes start at {{code|0}}.
exist in the [[JSON Array]] then {{code|-1}} is returned. All [[JSON Array]] indexes start at {{code|0}}.


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: jarr = json.indexOf(jarr, value)]
json.indexOf(array, value)
</source>
</source>
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: jarr = json.indexOf(jarr, value, startIndex)]
json.indexOf(array, value, start)
</source>
</source>
'''Parameters'''
'''Parameters'''
* {{code|jarr}} - The [[JSON Array]] to search.
{{param|array|The [[JSON Array]] to search.}}
* {{code|value}} - The value to find the index of in the [[JSON Array]].
{{param|value|The value to find the index of in the [[JSON Array]].}}
* {{code|startIndex}} - The index to start searching from, if not specified it defaults to 0.
{{param|start|The index to start searching from, if not specified it defaults to {{code|0}}.}}


|examples=
Find the index of the first occurrence of {{code|1}}:
<source lang="mtmacro" line>
[r: json.indexOf("[1,2,3,1,1,3]", 1)]
</source>
Returns: {{code|0}}


Find the index of the first occurrence of {{code|1}}, starting at index {{code|1}}:
<source lang="mtmacro" line>
[r: json.count("[1,2,3,1,1,3]", 1, 1)]
</source>
Returns: {{code|3}}


|example=
Find the index of the first occurrence of {{code|2}}, starting at index {{code|2}}:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[r: json.indexOf("[1,2,3,1,1,3]", 1)]
[r: json.count("[1,2,3,1,1,3]", 2, 2)]
[r: json.count("[1,2,3,1,1,3]", 1, 1)]
</source>
</source>
Returns: {{code|-1}}
|also=
{{func|json.count}}


Returns
0
3
}}
}}
[[Category:JSON Function]]
[[Category:JSON Function]]

Revision as of 19:28, 31 March 2009

json.indexOf() Function

Introduced in version 1.3b53
Returns the index of the first occurrence of a value in the JSON Array. If the value does not exist in the JSON Array then -1 is returned. All JSON Array indexes start at 0.

Usage

json.indexOf(array, value)
json.indexOf(array, value, start)

Parameters

  • array - The JSON Array to search.
  • value - The value to find the index of in the JSON Array.
  • start - The index to start searching from, if not specified it defaults to 0.

Examples

Find the index of the first occurrence of 1:
[r: json.indexOf("[1,2,3,1,1,3]", 1)]

Returns: 0

Find the index of the first occurrence of 1, starting at index 1:

[r: json.count("[1,2,3,1,1,3]", 1, 1)]

Returns: 3

Find the index of the first occurrence of 2, starting at index 2:

[r: json.count("[1,2,3,1,1,3]", 2, 2)]
Returns: -1

See Also