最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to add previous value of variable using JavaScript map - Stack Overflow

programmeradmin6浏览0评论

I'm using JavaScript map for getting all values from this type of data:

var data = {
    size: 600,
    sectors: [
        {
            percentage: 0.7,
            label: 'Thing 1'
        },
        {
            percentage: 0.3,
            label: "Thing Two"
        }
    ]
}

and in data.sectors.map( function(item,key) I'm calculating angle using percentage. Right know my question is how to save value from map to add this value to next calculation with map?

What I've got is every time map run I recive angle to calculate function for example:

1: 25
2: 30
3: 25
4: 20

And this is the angle which I use to calculate one function but in other function I have to calucalte angle using also previus value of it:

1: 25
2: 55
3: 80
4: 100

How to get this using map in JS?

I'm using JavaScript map for getting all values from this type of data:

var data = {
    size: 600,
    sectors: [
        {
            percentage: 0.7,
            label: 'Thing 1'
        },
        {
            percentage: 0.3,
            label: "Thing Two"
        }
    ]
}

and in data.sectors.map( function(item,key) I'm calculating angle using percentage. Right know my question is how to save value from map to add this value to next calculation with map?

What I've got is every time map run I recive angle to calculate function for example:

1: 25
2: 30
3: 25
4: 20

And this is the angle which I use to calculate one function but in other function I have to calucalte angle using also previus value of it:

1: 25
2: 55
3: 80
4: 100

How to get this using map in JS?

Share Improve this question edited Jan 20, 2017 at 8:46 Nikalaj asked Jan 20, 2017 at 7:22 NikalajNikalaj 1552 gold badges2 silver badges6 bronze badges 2
  • You want to update data.sectors.map in data with map, right? – Adam Azad Commented Jan 20, 2017 at 7:24
  • I'm updating data.sectors with map using push, but I have to get value from first in calculating map for example A and add this in a new calculation. Like in first move var A = 10 and in second we have got a new A = 25 but I need to add previues A to this A to get A = 35 to calculate in second 'loop' and in third 'loop' A = 35 + A – Nikalaj Commented Jan 20, 2017 at 7:28
Add a ment  | 

2 Answers 2

Reset to default 17

I understand this question is older, but I just ran into a similar problem and somehow couldn't find other Q/A's regarding this.

By using all three arguments in Array.prototype.map()'s callback: currentValue, index, array you are able to update the current value by using the previous value(s), as follows:

let newValues = [25,30,25,20].map((curr, i, array) => {
  return array[i] += array[i-1] ? array[i-1] : 0
})

console.log(newValues)
// expected: [25,55,80,100]

Note, similar to Array.prototype.reduce() you need to set an initial value when array[i-1] does not exist (in the first iteration of map). Otherwise, you use the previous value.

Javascript map function is used where you want to map each value of the array to a new value, it returns a new array. If what you want to do is to get a single value from the array, for example, add all the values of the array, you should use Javascript reduce function.

data.sectors.reduce((accumulator, each)=>{
   return (accumulator + each )
})

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Use this for more insight into the reduce function.

发布评论

评论列表(0)

  1. 暂无评论