Languages
[Edit]
EN

JavaScript - basic array operations (map, filter, find, fill, some, every methods)

3 points
Created by:
May87
827

Hello!
In this blog post, I've wanted to show you some basic array operations with simple examples.

1. map() method

In this example, I've used the map() method to create a new array filled with the results of calling a provided function on every element in the calling array.

// ONLINE-RUNNER:browser;

const array1 = ['🟦', '🟦', '🟦', '🟦'];

const array2 = array1.map((item) => '🔵');

console.log('array1: ' + array1);
console.log('array2: ' + array2);

2. filter() method

In this example, I've used the filter() method to create a new array filled with all elements that pass the test implemented by the provided function.

// ONLINE-RUNNER:browser;

const array1 = ['🟦', '🔵', '🟦', '🟦'];

const array2 = array1.filter((item) => item == '🟦');

console.log('array1: ' + array1);
console.log('array2: ' + array2);

3. find() method

In this example, I've used the find() method to get the first element in the provided array that satisfies the provided testing function.

// ONLINE-RUNNER:browser;

const array = ['🟦', '🟦', '🔵', '🔵'];

const item = array.find((item) => item == '🔵');

console.log('array: ' + array);
console.log('item: ' + item);

Note:

If there's no value that satisfies the testing function, undefined is returned.

4. fill() method

In this example, I've used the fill() method to change all elements in an array to a specific value, from a start index 1 (default is 0) to an end index (default array.length).

// ONLINE-RUNNER:browser;

const array = ['🟦', '🟦', '🟦', '🟦'];
console.log('array: ' + array);

array.fill('🔵', 1); // filling since index 1
console.log('array: ' + array);

array.fill('🔵');    // filling since index 0
console.log('array: ' + array);

5. some() method

In this example, I've used the some() method to test whether at least one element in the array passes the test implemented by the provided function. The method returns true if, in the array, it finds at least one element for which the provided function returns true.

// ONLINE-RUNNER:browser;

const array = ['🟦', '🔵', '🟦', '🔵'];

const result = array.some((item) => item == '🟦');

console.log('array: ' + array);
console.log('result: ' + result);

6. every() method

In this example, I've used the every() method to test whether all elements in the array pass the test implemented by the provided function. The method returns true or false.

// ONLINE-RUNNER:browser;

const array = ['🟦', '🟦', '🟦', '🔵'];

const result = array.every((item) => item == '🔵');

console.log('array: ' + array);
console.log('result: ' + result);

References

  1. Array.prototype.map() - JavaScript | MDN
  2. Array.prototype.filter() - JavaScript | MDN
  3. Array.prototype.find() - JavaScript | MDN
  4. Array.prototype.fill() - JavaScript | MDN
  5. Array.prototype.some() - JavaScript | MDN
  6. Array.prototype.every() - JavaScript | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join