Equivalent to in_array() to use in Javascript

Asked

Viewed 28 times

0

To know if a variable is in an array.

There is something I can use as if it were PHP in_array in Javascript?

Example of how it would look in PHP:

$cores = ['azul', 'verde', 'amarelo'];
$corParaBuscar = 'verde';
$corEncontradaNoArray = in_array($corParaBuscar, $cores);

Result -> $colorFind

1 answer

1

You can use javascript includes method:

let cores = ['azul', 'verde', 'amarelo'];
let corParaBuscar = 'verde';
let corEncontradaNoArray = cores.includes(corParaBuscar);
console.log(corEncontradaNoArray);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.