javascript find first code example

Example 1: javascript take first element of array

const array = [1,2,3,4,5];
const firstElement = array.shift();

// array -> [2,3,4,5]
// firstElement -> 1

Example 2: javascript find object by property in array

// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');

Example 3: javascript find object in array

myArray.findIndex(x => x.id === '45');

Example 4: array find

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

Example 5: javascript get first element of array

let array = ["a", "b", "c", "d", "e"];

// Both first1 and first2 have the same value.
let [first1] = array;
let first2 = array[0];

Example 6: find in js

The first element that will be found by that function
const f = array1.find(e => e > 10);

Tags:

Php Example