List : Select certain elements in list (JavaScript)

From Unify Community Wiki
Revision as of 04:02, 11 November 2012 by Hachibei (Talk | contribs)

Jump to: navigation, search

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


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

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox