JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page
  1. Reducing arrays of objects

Third Person Shooter Example

Imagine you are building a third-person shooter game, and you have an array of objects that represent the player's weapons. Each weapon object has the following properties:

  • name: the name of the weapon

  • damage: the base damage dealt by the weapon

  • accuracy: a value between 0 and 1 representing the weapon's accuracy

  • ammo: the number of rounds left in the weapon's magazine

You can use the Array.prototype.reduce() method to calculate the total damage dealt by all of the player's weapons that have ammo remaining.

Here is an example of how you could do this:

const weapons = [
  { name: 'Pistol', damage: 20, accuracy: 0.8, ammo: 12 },
  { name: 'Rifle', damage: 30, accuracy: 0.7, ammo: 8 },
  { name: 'Sniper rifle', damage: 50, accuracy: 0.9, ammo: 5 },
  { name: 'Shotgun', damage: 40, accuracy: 0.6, ammo: 0 }
];

const totalDamage = weapons.reduce((accumulator, weapon) => {
  if (weapon.ammo > 0) {
    return accumulator + weapon.damage;
  }
  return accumulator;
}, 0);

console.log(totalDamage); // 100

In this example, the reduce() function is called on the weapons array with two arguments: a callback function and an initial accumulator value of 0. The callback function has two arguments: accumulator and weapon.

On each iteration, the callback function checks if the current weapon has ammo remaining (weapon.ammo > 0). If it does, the callback function adds the damage property of the weapon object to the accumulator and returns the new value. If the weapon has no ammo remaining, the callback function simply returns the current value of the accumulator without modifying it. This process continues until all elements in the array have been processed, and the final value of accumulator is returned as the total damage dealt by all weapons with ammo remaining.

PreviousReducing arrays of objectsNextTry....catch

Last updated 2 years ago

🫐