json.count: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Clarified index start value)
m (Minor formatting changes, corrected examples, added see also.)
Line 4: Line 4:
|description=
|description=
Returns the number of occurrences of a value in a [[JSON Array]]. If the value does
Returns the number of occurrences of a value in a [[JSON Array]]. If the value does
not occur in the [[JSON Array]] then {{code| -1}} is returned. The index for the [[JSON Array]]
not occur in the [[JSON Array]] then {{code|-1}} is returned. The index for the [[JSON Array]]
starts at 0.
starts at {{code|0}}.


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: jarr = json.count(jarr, value)]
json.count(array, value)]
</source>
</source>
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: jarr = json.count(jarr, value, startIndex)]
json.count(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 count the occurrences of.
{{param|value|The value to count the occurrences of.}}
* {{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}}.}}
 
 


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


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

Revision as of 19:21, 31 March 2009

json.count() Function

Introduced in version 1.3b53
Returns the number of occurrences of a value in a JSON Array. If the value does

not occur in the JSON Array then -1 is returned. The index for the JSON Array

starts at 0.

Usage

json.count(array, value)]
json.count(array, value, start)]

Parameters

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

Example

Find the number of occurrences of 1:
[r: json.count("[1,2,3,1,1,3]", 1)]

Returns 3

Find the number of occurrences of 1, starting at index 1:

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

See Also