Multiply only specific value in a JavaScript object?

The ability to multiply only specific values in a JavaScript object can be a useful tool for performing calculations or making modifications to data. By using methods like map(), forEach(), and traditional loops, you can efficiently target and modify values that meet certain criteria.

An object in JavaScript is a separate entity having properties and a type. This article demonstrates various approaches to selectively multiply values within objects and arrays.

Method 1: Using Array.map() to Transform Object Properties

In this example, we multiply the value property of each object in an array by 3:

<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <script>
      let someArray = [
         {"car":"BMW", "value":34}, 
         {"car":"BENZ", "value":80},
         {"car":"AUDI", "value":75}
      ];
      
      let result = someArray.map(data => {
         return {...data, value: data.value * 3}
      });
      
      document.getElementById("tutorial").innerHTML = JSON.stringify(result);
   </script>
</body>
</html>
[{"car":"BMW","value":102},{"car":"BENZ","value":240},{"car":"AUDI","value":225}]

Method 2: Using Object.values() and forEach()

This approach uses Object.values() to iterate through nested objects and multiply specific array elements:

<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <script>
      let fundata = {
         'arr1': {
            a: [2, 3],
            b: []
         },
         'arr2': {
            a: [5, 6],
            b: []
         }
      };
      
      let multiplier = 50;
      Object.values(fundata).forEach(item => {
         item.a = item.a.map(number => number * multiplier);
      });
      
      document.getElementById("tutorial").innerHTML = JSON.stringify(fundata);
   </script>
</body>
</html>
{"arr1":{"a":[100,150],"b":[]},"arr2":{"a":[250,300],"b":[]}}

Method 3: Combining Array.map() and Array.reduce()

This example multiplies object properties to calculate areas, then sums the results:

<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <p id="tutorial1"></p>
   <script>
      var array = [
         { x: 10, y: 30 },
         { x: 20, y: 90 },
         { x: 54, y: 32 }
      ];
      
      var areas = array.map(arr => arr.x * arr.y);
      var total = areas.reduce((a, b) => (a + b));
      
      document.getElementById("tutorial").innerHTML = "Areas: " + JSON.stringify(areas);
      document.getElementById("tutorial1").innerHTML = "Total: " + total;
   </script>
</body>
</html>
Areas: [300,1800,1728]
Total: 3828

Method 4: Conditional Multiplication Using for Loop

This approach multiplies values only when they meet specific criteria:

<!DOCTYPE html>
<html>
<body>
   <p id="tutorial"></p>
   <p id="tutorial1"></p>
   <p id="tutorial2"></p>
   <p id="tutorial3"></p>
   <script>
      var employee = [
         { name: "John", amount: 800 },
         { name: "David", amount: 500 },
         { name: "Bob", amount: 450 }
      ];
      
      document.getElementById("tutorial").innerHTML = "Before multiplying:";
      document.getElementById("tutorial1").innerHTML = JSON.stringify(employee);
      
      // Multiply amount by 2 only if greater than 500
      for (var index = 0; index < employee.length; index++) {
         if (employee[index].amount > 500) {
            employee[index].amount = employee[index].amount * 2;
         }
      }
      
      document.getElementById("tutorial2").innerHTML = "After multiplying:";
      document.getElementById("tutorial3").innerHTML = JSON.stringify(employee);
   </script>
</body>
</html>
Before multiplying:
[{"name":"John","amount":800},{"name":"David","amount":500},{"name":"Bob","amount":450}]
After multiplying:
[{"name":"John","amount":1600},{"name":"David","amount":500},{"name":"Bob","amount":450}]

Comparison of Methods

Method Use Case Modifies Original Returns New Object
Array.map() Transform all elements No Yes
Object.values() + forEach() Modify nested objects Yes No
for loop Conditional modifications Yes No

Conclusion

JavaScript provides multiple approaches to multiply specific values in objects. Use Array.map() for creating new transformed arrays, forEach() for modifying existing objects, and traditional loops for conditional operations. Choose the method that best fits your specific use case and data structure.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements