Array methods that you should know

1- findIndex

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

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


This content originally appeared on DEV Community and was authored by Abdullah Furkan Özbek

1- findIndex

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

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

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3
  • find() method, which returns the value of an array element, instead of its index.
  • Returns The index of the first element in the array that passes the test. Otherwise, -1.
// EXAMPLE 1
function isPrime(num) {
  for (let i = 2; num > i; i++) {
    if (num % i == 0) {
      return false;
    }
  }
  return num > 1;
}

console.log([4, 6, 8, 9, 12].findIndex(isPrime)); // -1, not found
console.log([4, 6, 7, 9, 12].findIndex(isPrime)); // 2 (array[2] is 7)
// EXAMPLE 2
const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];

const index = fruits.findIndex(fruit => fruit === "blueberries");

console.log(index); // 3
console.log(fruits[index]); // blueberries

2- includes

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false
  • When comparing strings and characters, includes() is case-sensitive.
  • Returns: A Boolean which is true if the value searchElement is found within the array (or the part of the array indicated by the index fromIndex, if specified).

3- isArray

The Array.isArray() method determines whether the passed value is an Array.

Array.isArray([1, 2, 3]);  // true
Array.isArray({foo: 123}); // false
Array.isArray('foobar');   // false
Array.isArray(undefined);  // false
  • Returns true if the value is an Array; otherwise, false.

4- Some

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true
  • Returns true if the callback function returns a truthy value for at least one element in the array. Otherwise, false.

Links


This content originally appeared on DEV Community and was authored by Abdullah Furkan Özbek


Print Share Comment Cite Upload Translate Updates
APA

Abdullah Furkan Özbek | Sciencx (2021-09-14T08:02:16+00:00) Array methods that you should know. Retrieved from https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/

MLA
" » Array methods that you should know." Abdullah Furkan Özbek | Sciencx - Tuesday September 14, 2021, https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/
HARVARD
Abdullah Furkan Özbek | Sciencx Tuesday September 14, 2021 » Array methods that you should know., viewed ,<https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/>
VANCOUVER
Abdullah Furkan Özbek | Sciencx - » Array methods that you should know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/
CHICAGO
" » Array methods that you should know." Abdullah Furkan Özbek | Sciencx - Accessed . https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/
IEEE
" » Array methods that you should know." Abdullah Furkan Özbek | Sciencx [Online]. Available: https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/. [Accessed: ]
rf:citation
» Array methods that you should know | Abdullah Furkan Özbek | Sciencx | https://www.scien.cx/2021/09/14/array-methods-that-you-should-know/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.