List : Select certain elements in list (JavaScript)

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(Description)
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 the code mashup:
 
Let's have the code mashup:
Line 57: Line 57:
  
  
This also works with an unsorted List. Cheers.
+
This also works with an unsorted List.
  
  

Revision as of 14:28, 13 November 2012

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) 04:52, 11 November 2012 (CET))

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox