Introduction

In JavaScript, a lot of people work with arrays, but most of them use loops to perform operations on the array. This is not the right way to do so and can break the flow if logic goes wrong. Also, it increases the complexity of the logic.
Instead of using loops, we can use the JavaScript Array methods which are map() , filter(), reduce(), find(), every() and some().

map()

map() allows you to perform the same operation on every element of an array. People often use the forEach loop for this purpose instead of map() but that may cause the issues later.

For example...
You can also use map() with an array of objects to return a single value of each object. Refer to the example below:
  1. let numbers = [2, 3, 4, 5, 6, 7, 8, 9]
  2. // we want to multiply each number of array with 2
  3. let multipliedNumbers = numbers.map((value) => {
  4. return value * 2
  5. })
  6. // multipliedNumbers = [4, 6, 8, 10, 12, 14, 16, 18]

filter()

Filter() lets you filter the elements of an array based on the condition imposed. Elements not passing the condition will be discarded from the result.
For example:
  1. const cars = [{
  2. model: 'ferrari portofino',
  3. price: 100000
  4. }, {
  5. model: 'maserati quattroporte',
  6. price: 140000
  7. }, {
  8. model: 'ford mustang',
  9. price: 170000
  10. }, {
  11. model: 'lamborghini gallardo',
  12. price: 200000
  13. }, {
  14. model: 'bentley mulsanne',
  15. price: 210000
  16. }, {
  17. model: 'rolls royce phantom',
  18. price: 260000
  19. }, {
  20. model: 'ferrari california',
  21. price: 320000
  22. }, ]
  23. // we want to filter cars with price greater than 150000
  24. const myCars = cars.filter((element) => {
  25. return element.price > 150000
  26. })
  27. /** myCars = [ {model: "ford mustang", price: 170000}
  28. {model: "lamborghini gallardo", price: 200000}
  29. {model: "bentley mulsanne", price: 210000}
  30. {model: "rolls royce phantom", price: 260000}
  31. {model: "ferrari california", price: 320000}
  32. ]
  33. */

reduce()

The reduce method allows you to perform an operation on two adjacent elements of an array and use that result with the next element of an array. People usually avoid using it and they prefer to create their own logic, but reduce can save a lot of their work and makes the function smaller.
Consider the example below:
  1. let myCars = [{
  2. model: 'ferrari california',
  3. price: 100000
  4. }, {
  5. model: 'maserati quattroporte',
  6. price: 140000
  7. }, {
  8. model: 'ford mustang',
  9. price: 170000
  10. }, {
  11. model: 'lamborghini aventador',
  12. price: 200000
  13. }, {
  14. model: 'bentley continental',
  15. price: 210000
  16. }, {
  17. model: 'rolls royce ghost',
  18. price: 260000
  19. }, {
  20. model: 'ferrari portifino',
  21. price: 320000
  22. }, ]
  23. // we want to know the total worth of myCars
  24. myCars = myCars.reduce((worth, car) => {
  25. return worth + car.price
  26. }, 0)
  27. // mycars = 1400000

find()

The find method seems similar to filter, but it gives us an object in response, unlike filter. It creates an object based on the condition imposed. If you use find method in an array, it will return the first matching object.

Consider the example below:
  1. let myCars = [{
  2. model: 'ferrari portofino',
  3. price: 100000
  4. }, {
  5. model: 'maserati quattroporte',
  6. price: 140000
  7. }, {
  8. model: 'ford mustang',
  9. price: 170000
  10. }, {
  11. model: 'lamborghini gallardo',
  12. price: 200000
  13. }, {
  14. model: 'bentley mulsanne',
  15. price: 210000
  16. }, {
  17. model: 'rolls royce phantom',
  18. price: 260000
  19. }, {
  20. model: 'ferrari california',
  21. price: 320000
  22. }, ]
  23. // we want to find the first car with price > 150000
  24. myCars = myCars.find((car) => {
  25. return car.price > 150000
  26. })
  27. // mycars = {model: 'ford mustang', price: 170000}

every()

The every method returns a Boolean value based on the condition imposed. It is used to check if all the elements of an array pass the condition or not.
Think of it as AND operator, the response will be true only if all the elements pass the criteria.
For example:
  1. let myCars = [{
  2. model: 'ferrari portofino',
  3. price: 100000
  4. }, {
  5. model: 'maserati quattroporte',
  6. price: 140000
  7. }, {
  8. model: 'ford mustang',
  9. price: 170000
  10. }, {
  11. model: 'lamborghini gallardo',
  12. price: 200000
  13. }, {
  14. model: 'bentley mulsanne',
  15. price: 210000
  16. }, {
  17. model: 'rolls royce phantom',
  18. price: 260000
  19. }, {
  20. model: 'ferrari california',
  21. price: 320000
  22. }, ]
  23. /**
  24. *
  25. we want to check if
  26. 1. all the cars have price > 150000
  27. 2. all the cars have price > 50000
  28. * */
  29. myCars = myCars.every((car) => {
  30. return car.price > 150000
  31. })
  32. // 1. mycars = false
  33. myCars = myCars.every((car) => {
  34. return car.price > 50000
  35. })
  36. // 2. mycars = true

some()

The some method also returns a Boolean value just like every, based on the condition imposed. It is used to check if any of the elements of an array passes the condition.
You may think of it as an OR operator, the response will be true if any of the elements pass the criteria.
For example:
  1. let myCars = [{
  2. model: 'ferrari portofino',
  3. price: 100000
  4. }, {
  5. model: 'maserati quattroporte',
  6. price: 140000
  7. }, {
  8. model: 'ford mustang',
  9. price: 170000
  10. }, {
  11. model: 'lamborghini gallardo',
  12. price: 200000
  13. }, {
  14. model: 'bentley mulsanne',
  15. price: 210000
  16. }, {
  17. model: 'rolls royce phantom',
  18. price: 260000
  19. }, {
  20. model: 'ferrari california',
  21. price: 320000
  22. }, ]
  23. /**
  24. *
  25. we want to check if any of the car has price > 300000
  26. * */
  27. myCars = myCars.some((car) => {
  28. return car.price > 150000
  29. })
  30. // mycars = true
Depending upon your requirement, you can use these methods.
In the example below, all the array methods are used on the same set of data.
  1. const cities = [{
  2. city: 'Gurgaon',
  3. population: 99000
  4. }, {
  5. city: 'Seattle',
  6. population: 108000
  7. }, {
  8. city: 'London',
  9. population: 91000
  10. }, {
  11. city: 'Milan',
  12. population: 100000
  13. }, {
  14. city: 'Athens',
  15. population: 120000
  16. }, {
  17. city: 'sydney',
  18. population: 88000
  19. }, {
  20. city: 'Toronto',
  21. population: 55000
  22. }, {
  23. city: 'Tokyo',
  24. population: 92000
  25. }, {
  26. city: 'Berlin',
  27. population: 110000
  28. }, {
  29. city: 'Jakarta',
  30. population: 11000
  31. }, {
  32. city: 'Moscow',
  33. population: 199000
  34. }, {
  35. city: 'Zurich',
  36. population: 65000
  37. }, ]
  38. /**
  39. * map function
  40. *
  41. * divide the population of each city by 1000
  42. */
  43. let mapResult = cities.map((city) => {
  44. return city.population / 1000
  45. })
  46. // mapResult = [99, 108, 91, 100, 120, 88, 55, 92, 110, 11, 199, 65]
  47. /**
  48. * filter function
  49. *
  50. * get cities with a population greater than 100000
  51. */
  52. let filterResult = cities.filter((city) => {
  53. return city.population > 100000
  54. })
  55. /*
  56. filterResult = [ {city: "Seattle", population: 108000},
  57. {city: "Athens", population: 120000},
  58. {city: "Berlin", population: 110000},
  59. {city: "Moscow", population: 199000}
  60. ]
  61. */
  62. /**
  63. * find function
  64. *
  65. * get a city with a population greater than 100000
  66. */
  67. let findResult = cities.find((city) => {
  68. return city.population > 100000
  69. })
  70. // findResult = {city: "Seattle", population: 108000}
  71. /**
  72. * reduce function
  73. *
  74. * get total population
  75. */
  76. let reduceResult = cities.reduce((population, city) => {
  77. return population + city.population
  78. }, 0)
  79. // reduceResult = 1138000
  80. /**
  81. * every function
  82. *
  83. * check if all the cities have population greater tha 50000
  84. *
  85. */
  86. let everyResult = cities.every((city) => {
  87. return city.population > 50000
  88. })
  89. // everyResult = false
  90. /**
  91. * some function
  92. *
  93. * check if any of the city have population greater tha 50000
  94. *
  95. */
  96. let someResult = cities.every((city) => {
  97. return city.population > 50000
  98. })
  99. // someResult = true
For any query or suggestion, you can get in touch with me @ https://twitter.com/nitinmanocha16