Javascript: Map, Filter, and Reduce.

1. map()

let newArray = data.map(callback(currentValue[, index[, array]]) {// return element for newArray, after executing something}[, thisArg]);
const tasks = [{   'name'     : 'Insert',   'duration' : 120
}, {
'name' : 'Update', 'duration' : 60}, { 'name' : 'Delete', 'duration' : 240}];
let taskName = []for (let i = 0, i < tasks.length, i += 1) {taskName.push(tasks[i].name)}console.log(taskName)
const taskName = tasks.map(task => task.name)console.log(taskName) 

2. filter()

let newArray = arr.filter(callback(currentValue[, index[, array]]) {   // return element for newArray, if true}[, thisArg]);
const wish = ['Hey', 'Howdy', 'Hi', 'Hola', 'Hello', 'Hie']const filteredResult = wish.filter(word => word.length < 3)console.log(filteredResult);
['Hey', 'Hi', 'Hie']
const filteredTasks = tasks.filter((task) => task.duration <= 120 )

3. reduce()

let newArray = arr.filter(callback(currentValue, accumulatedValue) {// return the accumulated value, given the current and previous  accumulated value}, initialValue[, thisArg]);
let numbers = [1, 2, 3, 4, 5, 6],total = 0;numbers.forEach(function (number) {total += number})console.log(total) // 21
const total = [1, 2, 3, 4, 5, 6].reduce((previous, current) => previous+current),0)console.log(total) // 21
const total = [1, 2, 3, 4, 5, 6].reduce( (previous, current, index) => {const val = previous + current;console.log("The previous value is " + previous + "; the current value is " + current + ", and the current iteration is " + (index + 1))return val}, 0)console.log("The loop is done, and the final value is " + total + ".")

Conclusion

🤠 Internet Cowboy | 💻 JS Aficionado | http://imharshpatel.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store