The
array_map()
, and
array_search()
functions are generally known and used in PHP. These functions are useful when working with arrays. JavaScript has similar functions, but with other syntax.
array.map()
The
array.map(callback)
method returns an array containing all the elements of
array after applying the
callback function to each one.
- Example:
var arr = [1, 4, 9, 16];
//creates an array with the values from arr, doubled
const arr2 = arr.map((v, i)=>v*2);
console.log(arr2); //[2, 8, 18, 32]
array.indexOf()
The JavaScript
array.indexOf(value)
method is equivalent to the php
array_search(value, array)
function, it searches the
array
for a given
value and returns the corresponding
index if successful, or -1 if none is found.
- Example:
let arr =['ab', 'cd', 90];
var ixv = arr.indexOf('cd');
if(ixv !=-1) document.write('<p>arr contains the value: cd, with index: '+ixv+'</p>');
Daily Test with Code Example
HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em><p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;#id {
font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;$vname = 8;
echo $vname;