js find number in array code example
Example 1: how to get element of an array in javascript
var value = [a,b,c,d];
var value_b = value[1];
Example 2: find element in array javascript
const simpleArray = [3, 5, 7, 15];
const objectArray = [{ name: 'John' }, { name: 'Emma' }]
console.log( simpleArray.find(e => e === 7) )
console.log( simpleArray.find(e => e === 10) )
console.log( objectArray.find(e => e.name === 'John') )
Example 3: function search in javascript array
function findInArray(ar, val) {
for (var i = 0,len = ar.length; i < len; i++) {
if ( ar[i] === val ) {
return i;
}
}
return -1;
}
var ar = ['Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin'];
alert( findInArray(ar, 'Rudi') );
alert( findInArray(ar, 'Coco') );
Example 4: js find value in array
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
Example 5: find in js
The first element that will be found by that function
const f = array1.find(e => e > 10);
Example 6: find number in array js
array.include(numberWhichYouWantToFInd);