www.webdeveloper.com
Results 1 to 4 of 4

Thread: Decreasing values of an array by one where value > x?

  1. #1
    Join Date
    Apr 2009
    Posts
    13

    Decreasing values of an array by one where value > x?

    Hi,

    I have an array similar to this:

    Code:
    0 -> 0
    1 -> 5
    2 -> 9
    3 -> 2
    4 -> 1
    5 -> 6
    6 -> 8
    7 -> 7
    ...
    There is no clear logic and that does not matter. I need to decrease the values of those by one where the value is higher than x. How would I do this?

    For instance, if x = 5, then that array becomes

    Code:
    0 -> 0
    1 -> 5
    2 -> 8
    3 -> 2
    4 -> 1
    5 -> 5
    6 -> 7
    7 -> 6
    ...
    All values higher than 5 were decreased by one.

    Can someone help me?

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    I have an array prototype method for doing things like this, similar to php's array_walk, just pass it the name of a function you have defined to do the job on each member of the array, each member of the array will have the function performed on it. Note the function defined, in this case alterNum() must accept an argument which will be the member of the array, and return a value.

    Code:
    <script type="text/javascript">
    Array.prototype.changeEach = function (func) {
        var z = 0;
        while (z < this.length) {
            if (this[z] instanceof Array) { //go multiple levels deep if member is another array
                this[z].changeEach(func);
            } else {
                this[z] = func(this[z]); //run the function on the individual member
            }
            z++;
        }
    };
    
    var arr = [0, 5, 9, 2, 1, 6, 8, 7];
    
    function alterNum(num) {
        return (typeof num == 'number' && num > 5) ? num - 1 : num;
    }
    
    arr.changeEach(alterNum);
    alert(arr); //0,5,8,2,1,5,7,6
    </script>

  3. #3
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Actually, I just got around to re-working that changeEach method (I've been meaning to, but forgot til now..) as it only works on numerical arrays. The newly modified version works on associative arrays also, might as well share it:


    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    Array.prototype.changeEach = function (func) {
        if (typeof func == 'function') {
            var prop;
            for (prop in this) {
                if (this[prop] instanceof Array) { //if member is another array, go multiple levels deep
                    this[prop].changeEach(func);
                } else { //run func on individual member
                    this[prop] = func(this[prop]);
                }
            }
        }
    };
    
    var arr = [0, 5, 9, 2, 1, 6, 8, 7];
    
    var arr2 = [];
    arr2["bob"] = 3;
    arr2["fred"] = 7;
    arr2["john"] = 5;
    arr2["sue"] = 22;
    arr2["multi"] = [4,8,12];
    
    
    function alterNum(num) {
        return (typeof num == 'number' && num > 5) ? num - 1 : num;
    }
    
    arr.changeEach(alterNum);
    alert(arr);
    
    arr2.changeEach(alterNum);
    alert(arr2.fred);
    alert(arr2.multi[2]);
    </script>
    
    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Wink Quick and Dirty ...

    While I think 'astupidname's script is a little more elegant,
    if you just need a quick and dirty way to reduce the array size
    take a look at this ...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>Depopulate List</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=206258
    
    var InitArray = [0, 5, 9, 2, 1, 6, 8, 7];
    var arr = InitArray;
    
    function ShowArr() {
      alert(arr.join('\n'));
    }
      
    function ReduceArray(a) {
      var tmp = [];
      for (i=0; i<a.length; i++) { 
        if (a[i] <= 5) { tmp.push(a[i]); }
      }
      arr = tmp;  
      ShowArr();
    }
    
    function Initialize(a) {
      arr = InitArray;
      ShowArr();
    }
    </script>
    </head>
    <body>
    <button onClick="ShowArr()">Current Array Contents</button>
    <button onClick="ReduceArray(arr)">Reduce Array</button>
    <button onClick="Initialize(arr)">Initialize</button>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles