List : Select certain elements in list (JavaScript)

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(Description)
Line 3: Line 3:
 
This is how to select certain Strings within a generic List in JS.
 
This is how to select certain Strings within a generic List in JS.
  
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;

Revision as of 04:02, 11 November 2012

Description

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

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. Cheers.


by Amirul Zin (Hachibei (talk) 04:52, 11 November 2012 (CET))

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox