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"><!--
// https://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 - https://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. Read Excel file data in PHP - PhpExcelReader (56711)
  2. Get and Modify content of an Iframe (12777)
  3. AJAX with POST and PHP (11915)
  4. SHA256 Encrypt hash in JavaScript (5787)
  5. Register and show online users and visitors (22779)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (448)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (352)
  3. Register and show online users and visitors (321)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (302)
  5. Insert, Select and Update NULL value in MySQL (214)