json.unique: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Minor formatting changes, added see also.)
No edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 7: Line 7:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
json.unique(array)
json.unique(array)
</source>
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
{{param|array|The [[JSON Array]] to return only unique values from.}}
{{param|array|The [[JSON Array]] to return only unique values from.}}
Line 15: Line 15:
|example=
|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
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
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.
the [[Introduction_to_Tokens#Token_Type|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.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: names1 = '["Hero", "Dragon"]']
[h: names1 = '["Hero", "Dragon"]']
[h: names2 = getNPCNames()]
[h: names2 = getNPCNames()]
[h: names = json.merge(names1, names2)]
[h: names = json.merge(names1, names2)]
[r: json.unique(names)]
[r: json.unique(names)]
</source>
</syntaxhighlight>
If {{code|{{func|getPCNames}}}} 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 {{code|["Policeman","Sidekick","Hero","Dragon"]}}
result of the above code will be {{code|["Policeman","Sidekick","Hero","Dragon"]}}
While the above example demonstrates the output of {{code|{{func|json.unique}}}} if you want to merge
two or more arrays and get the unique values in one step you can use the {{code|{{func|json.union}}}}
function.


|also=
|also=
{{func|json.merge}}
{{func|json.merge}}
{{func|json.union}}


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

Latest revision as of 23:59, 15 March 2023

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"]

While the above example demonstrates the output of json.unique() if you want to merge two or more arrays and get the unique values in one step you can use the json.union()

function.

See Also