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 is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
array_map and array_search in JavaScript

Last accessed pages

  1. Diamonds Chase (235)
  2. Dog Mahjong (6631)
  3. Mahjong Games (19943)
  4. Colors In The Sky 2 (1496)
  5. Farm Frenzy2 (384)

Popular pages this month

  1. Flash Games - Free online Games (1360)
  2. Qwop (1158)
  3. Tunnel Rush (1062)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (983)
  5. Drag Racer V3 (758)