DEV Community

chenge
chenge

Posted on

6 2

Short Video: Array Iteration

JS Nuggets channel is nice with short videos for JS.

Following codes shared with you.

//forEach
[1,2,3].forEach(function(item, index){
  console.log(item, index)
})

//map
const three = [1,2,3]
const doubled = three.map(function(item){
  return item * 2
})
console.log(doubled)

//filter
const ints = [1,2,3]
const evens = ints.filter(function(item){
  return item % 2 === 0
})
console.log(evens)

//reduce
const sum = [1,2,3].reduce(function(result, item){
  return result + item
})
console.log(sum)

//some
const hasNegativeNumbers = [1,2,3,-1,4].some(function(item){
  return item < 0
})
console.log(hasNegativeNumbers)

//every
const allPositiveNumbers = [1,2,3].every(function(item){
  return item > 0
})
console.log(allPositiveNumbers)

//find
const objects = [{id: 'a'}, {id: 'b'}, {id: 'c'}]
const found = objects.find(function(item){
  return item.id === 'b'
})
console.log(found)

//find index
const objects2 = [{id: 'a'}, {id: 'b'}, {id: 'c'}]
const foundIndex = objects2.findIndex(function(item){
  return item.id === 'b'
})
console.log(foundIndex)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay