Can't cleanly break out of the for loop in javascript

Can't cleanly break out of the for loop in javascript

Got this simple example:

Got this simple example:

const arr = [
  {name: 'first', amount: 2},
  {name: 'second', amount: 1},
  {name: 'third', amount: 1}
]
const obj = {name: 'second', amount: 3}

for (let i = 0; i < arr.length; i++) { if (arr[i].name === obj.name) { arr[i].amount += obj.amount; break; } else { arr.push(obj) } }

I want to execute the ordinary for loop and the desired result is that if there's an object with the same name as in the array, then just add up the amount, else I want obj to be pushed to the list. But in fact both events happen. How to make so when I break out from if statement the else statement won't run?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Clone an Array in JavaScript?

In this JavaScript tutorial, you'll learn how to clone an array in JavaScript. Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array.

JavaScript Array.flatMap()

JavaScript Array.flatMap(): Use flatMap to map over an array and flatten the result array in one go! Now lets up the game and introduce "flatMap". It combines the steps of first mapping over the array with `map()` & then calling `flat()`. Instead of calling 2 methods, just use `flatMap()` 👍