Dshiz
Dshiz

Reputation: 3341

How to use Array.map() to find the differences between each subsequent element in an array

I am trying to return an array of differences between each subsequent number in the array [1,3,5,7,11,13].

I have the following:

let arr = [1,3,5,7,11,13]
let differences = diff(arr)

function diff(arr){
    let diffs = arr.map((a, b) => a - b)
    return diffs
}

console.log(differences)
//Should be: [2,2,2,4,2]

It should return [2,2,2,4,2], but it's returning [1,2,3,4,7,8] instead.

How do I use Array.map() correctly to return the expected result of [2,2,2,4,2]?

Upvotes: 0

Views: 970

Answers (5)

PM_sudo
PM_sudo

Reputation: 21

Try this

let arr = [1,3,5,7,11,13]
function diffs(arr){
    let diffs = arr
          .slice(0,-1)
          .map((v, i, a) => ((i===a.length-1)?arr[arr.length-1]:a[i+1])-v)
    return diffs
}
let differences = diffs(arr)
console.log(`[${differences.join(',')}]`);

Upvotes: 0

Shreyash bhatt
Shreyash bhatt

Reputation: 156

If you are specific to use array map only for solving the problem. This code will work for you

let arr = [1,3,5,7,11,13]
let differences = diff(arr)

function diff(arr){
    let diffs = arr.map((elem, index, array) => {
      if(index > 0) return elem - array[index - 1];
    })
    return diffs.slice(1)
}

console.log(differences)

Although i would strongly suggest you to use for..in or forEach or simple loop for more clean code.

Upvotes: 0

Jiří Cihelka
Jiří Cihelka

Reputation: 378

The callback of the Array.map function has 3 parameters, 2 of which are optional - element, index and array. In this case you map the array to the difference of the element and the index, not the element and the next element. If you want to return an array of the differences you can use something like this:

let arr = [1,3,5,7,11,13];
let differences = diffs(arr);

function diffs(arr){
    let diffs = arr.map((element, index, arr) => (arr[index+1] || 0)  - element);
    diffs.pop();
    return diffs;
}

console.log(differences);

Upvotes: 0

Felipe Malara
Felipe Malara

Reputation: 1914

The map function takes three parameters

  • element
  • index
  • array

Take a deeper look in here -> map

So if you need to have an array of differences you will need something like this:

array
.slice(1) // you slice to start calculating the differences from the second number
.map((element, index, newArray) => 
    element - array[index] // if index === 0, element shall be 3 now, and array[0] is 1
)

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370989

(a, b) => a - b doesn't make any sense, since the first argument is the number being iterated over, and the second is the index being iterated over (eg, 0, 1, 2, 3...).

Slice off the first or last element, then use the index to subtract the item being iterated over from one of the adjacent items in the original array:

let arr = [1,3,5,7,11,13]
let differences = diffs(arr)

function diffs(arr){
  return arr
    .slice(1)
    .map((num, i) => num - arr[i]);
}

console.log(differences)

Upvotes: 2

Related Questions