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 HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
array_map and array_search in JavaScript

Last accessed pages

  1. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (41724)
  2. Get Duration of Audio /Video file before Upload (5913)
  3. Follow the mouse cursor with a DIV inside a Parent (2703)
  4. Register and show online users and visitors (23278)
  5. Node.js Move and Copy Directory (1127)

Popular pages this month

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