Array Utility functions : Array « Language Basics « JavaScript DHTML






Array Utility functions

  
/*
JavaScript Application Cookbook
By Jerry Bradenbaugh

Publisher: O'Reilly 
Series: Cookbooks
ISBN: 1-56592-577-7
*/ 

<HTML>
<HEAD>
<TITLE>arrays.js Example</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
// arrays.js

// Add up the values of all the elements, then 
// divide that sum by the number of elements
function avg(arrObj) {
  var sum = 0;
  for (var i = 0; i < arrObj.length; i++) {
    sum += arrObj[i];
    }
  return (sum / i);
  }

// Iterate through the elements, 
// and return the highest
function high(arrObj) {
  var highest = arrObj[0];
  for (var i = 1; i < arrObj.length; i++) {
    highest = (arrObj[i] > highest ? arrObj[i] : highest);
    }
  return (highest);  
  }

// Iterate through the elements, 
// and return the lowest
function low(arrObj) {
  var lowest = arrObj[0];
  for (var i = 1; i < arrObj.length; i++) {
    lowest = (arrObj[i] < lowest ? arrObj[i] : lowest);
    }
  return (lowest);  
  }

// Iterate through the elements, amd perform
// a string replacement on each
function jsGrep(arrObj, regexp, subStr) {
  for (var i = 0; i < arrObj.length; i++) {
    arrObj[i] = arrObj[i].replace(regexp, subStr);
    }
  return arrObj;
  }

// This function returns a copy of an array object
// with the last element removed
function truncate(arrObj) {
  arrObj.length = arrObj.length - 1;
  return arrObj;
  }


// This function returns a copy of an array
// object with the first element removed
function shrink(arrObj) {
  var tempArray = new Array(); 
  for(var p = 1; p < arrObj.length; p++) {
    tempArray[p - 1] = arrObj[p];
    }
  return tempArray;
  }


// This function returns a copy of an array 
// object with the elements of another array 
// object added beginning from a specified index.
function integrate(arrObj, elemArray, startIndex) {
  startIndex = (parseInt(Math.abs(startIndex)) < arrObj.length ? parseInt(Math.abs(startIndex)) : arrObj.length);
  var tempArray = new Array(); 
  for( var p = 0; p < startIndex; p++) {
    tempArray[p] = arrObj[p];
    }
  for( var q = startIndex; q < startIndex + elemArray.length; q++) {
    tempArray[q] = elemArray[q - startIndex];
    }
  for( var r = startIndex + elemArray.length; r < (arrObj.length + elemArray.length); r++) {
    tempArray[r] = arrObj[r - elemArray.length];
    }
  return tempArray;
  }

// This function returns an array reordered according to a multiple 
// passed in by the user. The defualt is 1.
function reorganize(formObj, stepUp) {
  stepUp = (Math.abs(parseInt(stepUp)) > 0 ? Math.abs(parseInt(stepUp)) : 1);
  var nextRound = 1;
  var idx = 0;
  var tempArray = new Array();

  for (var i = 0; i < formObj.length; i++) {
    tempArray[i] = formObj[idx];
    if (idx + stepUp >= formObj.length) {
      idx = nextRound;
      nextRound++;
      }
    else {
      idx += stepUp;
      }
    }
  return tempArray;
  }
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript1.2">
<!--

var someArray = new Array(1,2,3,.1098,5,2,3.456,1324.55,-0.76,45,3,47.234,.00060,65.7,1,3,2,4,55);
var grepExample = new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');
document.write("<B>Original Array: " + someArray + "</B><BR>" + 
  "Average: " + avg(someArray) + "<BR>" + 
  "Lowest: " + low(someArray) + "<BR>" + 
  "Highest: " + high(someArray) + "<BR>" + 
  "Truncate by 1: " + truncate(someArray) + "<BR>" + 
  "Shrink by 1: " + shrink(someArray) + "<BR>" + 
  "Reorganize(by 4): " + reorganize(someArray, 4) + "<BR>" + 
  "Integrate ('An element', 'Another one', and 'One more', at index 5): " + 
  integrate(someArray, new Array('An element', 'Another one', 'One more'), 5) + "<BR><BR>" + 
  "<B>Original grepExample Array: " + grepExample + "</B><BR> jsGrep(grepExample, /day/, \'day Night\'): " + 
  jsGrep(grepExample, /day/, 'day Night') + "<BR>");

//-->
</SCRIPT>


</BODY>
</HTML>
           
         
    
  








Related examples in the same category

1.Demo all methods in Array
2.Assing array value inside function
3.Simple Array Demo
4.Array loop, find:Control array : Two dimension array
5.Reversing, Sorting, and Concatenating an Array
6.Custom Numeric Comparison for the Array.Sort Method
7.Case-Insensitive Comparison for the Array.Sort Method
8.Iterating Through a Sparse Array
9.Using Functions to Iterate Through an Array
10.Reading and Writing Array Elements
11.Array with a numeric parameter and assign data to it
12.A string array
13.Array - properties and methods:length, join, reverse, push,pop,shift
14.Array - sort()
15.Array - concat and slice
16.Array - splice
17.Methods and Properties of the Array Object
18.Displaying the Contents of an Array
19.Using the Array.join() Method
20.Using JavaScript Arrays
21.Extending the Length of an Array
22.An Array within an Array
23.Using the Methods of the Array object
24.Array.sort() Possibilities
25.Array.reverse() Method
26. Array Concatenation
27.A Looping Array Lookup
28.A Simple Parallel Array Lookup
29.Adding a prototype Property
30.Two-Dimensional Array Work Around
31.Array definition and iteration
32.Reference an Array by index
33.URL Array
34.Dynamic array
35.Use for loop to display elements in an array
36.A string array variable
37.Queue based on array