List : Select certain elements in list (JavaScript)

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(Description)
 
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
 
==Description==
 
==Description==
  
This is how to select certain Strings within a generic List in JS.
+
This is how to select certain elements within a generic List in JS. In this example, List containing Strings type is used.
  
Let's have a the code mashup:
+
Let's have the code mashup:
  
 
<syntaxhighlight lang="javascript">import System.Collections.Generic;
 
<syntaxhighlight lang="javascript">import System.Collections.Generic;
Line 28: Line 28:
 
return counter;
 
return counter;
 
}
 
}
 +
</syntaxhighlight>
  
 +
Then, in the same file:
 +
<syntaxhighlight lang="javascript">
 
//Game start
 
//Game start
  
Line 57: Line 60:
  
  
by Amirul Zin ([[User:Hachibei|Hachibei]] ([[User talk:Hachibei|talk]]) 04:52, 11 November 2012 (CET))
 
  
 
[[Category: Utility Scripts]]
 
[[Category: Utility Scripts]]
 
[[Category: JavaScript]]
 
[[Category: JavaScript]]
 +
 +
by Amirul Zin [[User:Hachibei|Hachibei]] ([[User talk:Hachibei|talk]]) 15:29, 13 November 2012 (CET)

Latest revision as of 14:29, 13 November 2012

[edit] Description

This is how to select certain elements within a generic List in JS. In this example, List containing Strings type is used.

Let's have the code mashup:

import System.Collections.Generic;
 
var apple : String = "Apple";
var banana : String = "Banana";
 
var elementList :List.<String> = new List.<String>();
 
//Counting function
 
var elements : String;
 
function listCount(foo) {
 
 	var counter : int = 0;
 
	for( elements in elementList ) {
	    if ( elements === foo ){
	    counter++;
	    }
	}
 
	return counter;
}

Then, in the same file:

//Game start
 
function Start(){ 
 
    elementList.Add(apple);
    elementList.Add(apple);
 
    elementList.Add(banana);
    elementList.Add(banana);
    elementList.Add(banana);
}
 
function Update (){
 
    if ( Input.GetKeyDown (KeyCode.C) ){ 
         Debug.Log("Apple: " + listCount(apple) ); 
    }
 
}
 
// Output: Apple: 2


This also works with an unsorted List.

by Amirul Zin Hachibei (talk) 15:29, 13 November 2012 (CET)

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox