
- 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
How to subtract elements of two arrays and store the result as a positive array in JavaScript?
Suppose, we have two arrays like these −
const arr1 = [1,2,3,4,5,6]; const arr2 = [9,8,7,5,8,3];
We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of the array.
Therefore, for these arrays, the output should look like −
const output = [8,6,4,1,3,3];
We will use a for loop and keep pushing the absolute difference iteratively into a new array and finally return the array.
Example
Following is the code −
const arr1 = [1,2,3,4,5,6]; const arr2 = [9,8,7,5,8,3]; const absDifference = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length; i++){ const el = Math.abs((arr1[i] || 0) - (arr2[i] || 0)); res[i] = el; }; return res; }; console.log(absDifference(arr1, arr2));
Output
This will produce the following output in console −
[ 8, 6, 4, 1, 3, 3 ]
- Related Articles
- How to store two arrays as a keyvalue pair in one object in JavaScript?
- Return the floor of the array elements and store the result in a new location in Numpy
- Return the ceil of the array elements and store the result in a new location in Numpy
- How to store MongoDB result in an array?
- Return the truncated value of the array elements and store the result in a new location in Numpy
- Add two consecutive elements from the original array and display the result in a new array with JavaScript
- How to sum elements at the same index in array of arrays into a single array? JavaScript
- How to add two arrays into a new array in JavaScript?
- How to swap two array elements in JavaScript?
- How to combine two arrays into an array of objects in JavaScript?
- How to Create an Array using Intersection of two Arrays in JavaScript?
- How to find the common elements between two or more arrays in JavaScript?
- How to store a key => value array in JavaScript?
- Program to find uncommon elements in two arrays - JavaScript
- Take in two 2-D arrays of numbers and returns their matrix multiplication result- JavaScript

Advertisements