Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (_arrCheckpoints.length > 0) {
    var _yr = new Array();
   for (var i = 0; i < _arrCheckpoints.length; i++) {
   var _Ayr = _arrCheckpoints[i].substring(_arrCheckpoints[i].length - 4) 
   _yr.push(_Ayr);
       }

}

  output _yr = 2014
               2014
               2014
               2013
               2013 
               2013


  i need _yr = 2014
               2013
Posted
Updated 8-Apr-14 20:06pm
v2

Please try:

Example that uses no libraries. This requires two new prototype functions, contains and unique
"Javascript
Array.prototype.contains = function(v) {
    for(var i = 0; i < this.length; i++) {
        if(this[i] === v) return true;
    }
    return false;
};

Array.prototype.unique = function() {
    var arr = [];
    for(var i = 0; i < this.length; i++) {
        if(!arr.contains(this[i])) {
            arr.push(this[i]);
        }
    }
    return arr;
}

You can then do:

var duplicates = [1,3,4,2,1,2,3,8];
var uniques = duplicates.unique(); // result = [1,3,4,2,8]

For more reliability, you can replace contains with MDN's indexOf shim and check if each element's indexOf is equal to -1: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf


Example from: click here
 
Share this answer
 
Comments
[no name] 9-Apr-14 2:14am    
Hello sankit
actually in my array list var, i am getting output like this
Output _yr = 2014
2014
2014
2013
2013
2013

please see my question i have updated it and plz help me
Sanket Saxena 9-Apr-14 2:39am    
why use var _yr = new Array();
you can use _yr[] like:

var _yr=[];
if (_arrCheckpoints.length > 0) {

for (var i = 0; i < _arrCheckpoints.length; i++) {
var _Ayr = _arrCheckpoints[i].substring(_arrCheckpoints[i].length - 4)
_yr.push(_Ayr);
}

seems like your _yr.push(_Ayr) throw error
C#
if (_arrCheckpoints.length > 0) {
                        var _yr = new Array();
                        var x = 0;
                        var _strHTML2 = "<div id='yrd'><ul class='nav'>";
                        for (var i = 0; i < _arrCheckpoints.length; i++) {

                            var _Ayr = _arrCheckpoints[i].substring(_arrCheckpoints[i].length - 4)

                            if (_yr != _Ayr) {

                                x++;
                                if (_yr[i-x] != _Ayr) {

                                    _yr.push(_Ayr);

                                }

                            }


                        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900