
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
When summing values from 2 arrays how can I cap the value in the new JavaScript array?
Suppose, we have two arrays both containing three elements each, the corresponding values of red, green, blue color in integer.
Our job is to add the corresponding value to form an array for new rgb color and also making sure if any value adds up to more than 255, we that value to 255.
Therefore, let’s define a function addColors() that takes in two arguments, both arrays and returns a new array based on the input.
The code for this will be −
Example
const color1 = [45, 125, 216]; const color2 = [89, 180, 78]; const addColors = (color1, color2) => { const newColor = color1.map((val, index) => { return val + color2[index] <= 255 ? val + color2[index] : 255; }) return newColor; }; console.log(addColors(color1, color2));
Output
The console output will be −
[ 134, 255, 255 ]
We map over the first color, add the corresponding value of the second color to it, if the value exceeds 255, we return 255 otherwise we return the added value. So in this way the addColors() function will do the job for us.
- Related Articles
- Combine unique items of an array of arrays while summing values - JavaScript
- Summing up unique array values in JavaScript
- Summing all the unique values of an array - JavaScript
- JavaScript - summing numbers from strings nested in array
- Create new array without impacting values from old array in JavaScript?
- How to add two arrays into a new array in JavaScript?
- Get the smallest array from an array of arrays in JavaScript
- Building a Map from 2 arrays of values and keys in JavaScript
- How can I check JavaScript arrays for empty strings?
- How can I remove a specific item from an array in JavaScript
- How to get single array from multiple arrays in JavaScript
- How to add new value to an existing array in JavaScript?
- How can I put a Java arrays inside an array?
- Summing array of string numbers using JavaScript
- Summing numbers from a string - JavaScript

Advertisements