json.unique: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: {{MacroFunction |name=json.unique |version=1.3b53 |description= Returns a JSON Array with each value that occurs in the source JSON Array occurring only once. The relative order o...)
 
m (Minor formatting changes, added see also.)
Line 3: Line 3:
|version=1.3b53
|version=1.3b53
|description=
|description=
Returns a [[JSON Array]] with each value that occurs in the source [[JSON Array]] occurring only once.  
Returns a [[JSON Array]] with each value that occurs in the source [[JSON Array]] occurring only once. The relative order of the values in the array may not be preserved.
The relative order of the values in the array may not be preserved.




|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: jarr = json.unique(jarr)]
json.unique(array)
</source>
</source>
'''Parameters'''
'''Parameters'''
* {{code|jarr}} - A [[JSON Array]].
{{param|array|The [[JSON Array]] to return only unique values from.}}


 
|example=
 
Lets say you have a [[JSON Array]] that contains the following [[Token]] names: {{code|["Hero", "Dragon"]}}, and you use {{code|{{func|getPCNames}}}} to return the names of
|examples=
the [[PC Token]]s, you could use the following code to generate a [[JSON Array]] that contains the values in both [[JSON Array]]s with no value present more than once.
Lets say you have a [[JSON Array]] that contains the following [[Token]] names.
{{code|["Hero", "Dragon"]}} and you use {{code|[[getPCNames|getPCNames()]]}} to return the names of
the [[PC Token]]s, you could use the following code to generate a [[JSON Array]] that contains the values in both [[JSON Arrays]] with no value present more than once.
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: names1 = '["Hero", "Dragon"]']
[h: names1 = '["Hero", "Dragon"]']
Line 28: Line 22:
[r: json.unique(names)]
[r: json.unique(names)]
</source>
</source>
If {{code|[[getPCNames|getPCName()]]}} returns {{code|["Hero", "Sidekick", "Policeman"]}} then the
If {{code|{{func|getPCNames}}}} returns {{code|["Hero", "Sidekick", "Policeman"]}} then the
result of the above code will be  
result of the above code will be {{code|["Policeman","Sidekick","Hero","Dragon"]}}
{{code|["Policeman","Sidekick","Hero","Dragon"]}}
 
|also=
{{func|json.merge}}


}}
}}
[[Category:JSON Function]]
[[Category:JSON Function]]

Revision as of 19:45, 31 March 2009

json.unique() Function

Introduced in version 1.3b53
Returns a JSON Array with each value that occurs in the source JSON Array occurring only once. The relative order of the values in the array may not be preserved.

Usage

json.unique(array)

Parameters

  • array - The JSON Array to return only unique values from.

Example

Lets say you have a JSON Array that contains the following Token names: ["Hero", "Dragon"], and you use getPCNames() to return the names of

the PC Tokens, you could use the following code to generate a JSON Array that contains the values in both JSON Arrays with no value present more than once.

[h: names1 = '["Hero", "Dragon"]']
[h: names2 = getNPCNames()]
[h: names = json.merge(names1, names2)]
[r: json.unique(names)]

If getPCNames() returns ["Hero", "Sidekick", "Policeman"] then the

result of the above code will be ["Policeman","Sidekick","Hero","Dragon"]

See Also