简体   繁体   English

如何使用 Array.map() 查找数组中每个后续元素之间的差异

[英]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] .我试图返回数组[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.它应该返回[2,2,2,4,2] ,但它返回的是[1,2,3,4,7,8]

How do I use Array.map() correctly to return the expected result of [2,2,2,4,2] ?如何正确使用Array.map()来返回[2,2,2,4,2]的预期结果?

(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...). (a, b) => a - b没有任何意义,因为第一个参数是被迭代的数字,第二个是被迭代的索引(例如,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)

The map function takes three parameters map function 需要三个参数

  • element元素
  • index指数
  • array大批

Take a deeper look in here -> map在这里深入了解-> 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
)

The callback of the Array.map function has 3 parameters, 2 of which are optional - element, index and array. Array.map function的回调有3个参数,其中2个是可选的——元素、索引和数组。 In this case you map the array to the difference of the element and the index, not the element and the next element.在这种情况下,您将 map 数组指向元素和索引的差异,而不是元素和下一个元素。 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);

If you are specific to use array map only for solving the problem.如果您专门使用数组 map 仅用于解决问题。 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.虽然我强烈建议您使用 for..in 或 forEach 或简单循环以获得更干净的代码。

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(',')}]`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM