List : Select certain elements in list (JavaScript)

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Generic List : Select and count certain String in list (JS))
 
(Description)
Line 52: Line 52:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
This also works with an unsorted List.
 
This also works with an unsorted List.
==
+
 
 +
 
 
by Amirul Zin ([[User:Hachibei|Hachibei]] ([[User talk:Hachibei|talk]]) 04:52, 11 November 2012 (CET))
 
by Amirul Zin ([[User:Hachibei|Hachibei]] ([[User talk:Hachibei|talk]]) 04:52, 11 November 2012 (CET))
 +
 +
[[Category: Utility Scripts]]
 +
[[Category: JavaScript]]

Revision as of 03:55, 11 November 2012

Description

This is how to select certain Strings within a generic List in JS.

Let's have a 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;
}
 
//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) 04:52, 11 November 2012 (CET))

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox