The array_map(), and array_search() functions are generally known and used in PHP. These functions are useful when working with arrays. JavaScript doesn't have such functions, but they can be created relativ easily.

array_map

array_map(callback, arr1) - returns an array containing all the elements of arr1 after applying the callback function to each one.
Code:
function array_map(func, array) {
  if(array.constructor.toString().match(/array/ig)) result = new Array();
  else if(array.constructor.toString().match(/object/ig)) result = new Object();
  if(result) {
    for(var indice in array) {
      result[indice] = eval(func+'('+array[indice]+')');
    }
    return result;
  }
}

Example:
<script type="text/javascript"><!--
// http://coursesweb.net/javascript/
function test(x) { return x*x; }       // callback function to test array_map

// array_map
function array_map(func, array) {
  if(array.constructor.toString().match(/array/ig)) result = new Array();
  else if(array.constructor.toString().match(/object/ig)) result = new Object();
  if(result) {
    for(var indice in array) {
      result[indice] = eval(func+'('+array[indice]+')');
    }
    return result;
  }
}

var arr1 = [0, 3, 88];
var arr2 = { 1:23, 'abc':12, 'xy_z':8 }     // associative array (object)

arr1 = array_map('test', arr1);
arr2 = array_map('test', arr2);

alert(arr1);               // 0, 9, 7744
alert(arr2['xy_z']);       // 64
--></script>

array_search

array_search(value, array) - searches the array for a given value and returns the corresponding key if successful.
Code:
function array_search(val, array) {
  if(typeof(array) === 'array' || typeof(array) === 'object') {
    var rekey;
    for(var indice in array) {
      if(array[indice] == val) {
        rekey = indice;
        break;
      }
    }
    return rekey;
  }
}

Example:
<script type="text/javascript"><!--
// array_search - http://coursesweb.net/javascript/
function array_search(val, array) {
  if(typeof(array) === 'array' || typeof(array) === 'object') {
    var rekey;
    for(var indice in array) {
      if(array[indice] == val) {
        rekey = indice;
        break;
      }
    }
    return rekey;
  }
}

var arr1 = [1, 3, 'js-php'];
var arr2 = { 1:23, 'abc':12, 'site':'coursesweb.net' };      // associative array (object)

alert( array_search(3, arr1) );                    // 1
alert( array_search('coursesweb.net', arr2) );     // site
--></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
array_map and array_search in JavaScript

Last accessed pages

  1. Select in MySQL, Output results in HTML Table (1419)
  2. Golf Putt It In (739)
  3. Disable button and Enable it after specified time (5129)
  4. Flash Games - Free online Games (22975)
  5. Move image from an element /tag to another (1637)

Popular pages this month

  1. Qwop (3219)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (2547)
  3. Get the value of the selected /checked checkboxes in a form (1608)
  4. Insert, Select and Update NULL value in MySQL (1423)
  5. Add, Change, and Remove Attributes with jQuery (1301)