Reproduce in plain Javascript the PHP function in_array(needle, haystack) -


if want skip number of items in for loop in php, might use php's native in_array(needle,haystack); function in following manner:

php

$fruit = array('apple','banana','cucumber','grapes','pear','tomato'); $salad = array('tomato','cucumber'); $herb = array('banana');  $countfruit = count($fruit);  ($i = 0; $i < $countfruit; $i++) { if (in_array($fruit[$i],$salad)) continue; $fruitbowl[] = $fruit[$i]; } 

this give me $fruitbowl of:

array('apple','banana','grapes','pear'); 

i realize use simpler

$fruitbowl = array_diff($fruit,$salad); 

but bear me - i'm making example go along.

how reproduce (or similar) in javascript?

here's have come with:

javascript

var fruit = ['apple','banana','cucumber','grapes','pear','tomato']; var salad = ['tomato','cucumber']; var herb = ['banana']; var fruitbowl = [];  var countfruit = fruit.length;  (var = 0; < countfruit; i++) { if (salad.indexof(fruit[i]) !== -1) {continue;} fruitbowl.push(fruit[i]); } 

having got far, question specifically this:

is javascript

if (salad.indexof(fruit[i]) !== -1) {continue;} 

now (in 2015) standard way reproduce

if (in_array($fruit[$i],$salad)) continue; 

in php?

or there less obscure way of going it?

n.b. understand jquery has inarray , prototype has array.indexof i'm looking plain javascript solution.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -