Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - How to set value in array depending on previous value of array

I've got a simple problem, but I'm struggling to find the easiest solution without transforming the array a hundred times. I want to do a simple stacked graph in google sheets, with weeks on X and values on Y. I got the values for each week, but only for weeks, that have a value.

The values are all calculations I've done with google apps script/ js.

person1 = [[2019/37,2], [2019/42,3]] and so on, for multiple persons and for 80 weeks in total.

The num value is the total value after each week. So I want the array to be filled up with the missing weeks. Therefore I mapped this to another array, where I have all the weeks but no values, giving these weeks the value 0:

person1= [[2019/37,2],[2019/38,0],[2019/39,0],...,[2019/42,3],[2019/43,0],[2019/44,0],...]

This of course does not fit to see a progress in the graph. So I need something to set the weeks, which were filled up, to the previous value, resulting in

person1= [[2019/37,2],[2019/38,2],[2019/39,2],...,[2019/42,3],[2019/43,3],[2019/44,3],...]

Looping through this and setting the values with something like person[i][1] == person[i-1][1] seems not to be a good practice of course. So, what would be the best way to achieve this? I'm kind of stuck with this now, I feel like I don't see the forest for the trees.

Thanks in advance!

code:

let valueArray =  [[2019/37,2], [2019/42,3]]
let weeksArray = [2019/38,2019/39,2019/40,2019/41...]
//find missing weeks
let notFound = weeksArray.filter(el => valueArray.includes(el) == false).map(x => [x,0]);

//concat and sort
let outArray = arr.concat(notFound).sort((a,b)=> a[0].localeCompare(b[0]));

//output:
//[[2019/37,2],[2019/38,0],[2019/39,0],...,[2019/42,3],[2019/43,0],[2019/44,0],...]
question from:https://stackoverflow.com/questions/65942029/how-to-set-value-in-array-depending-on-previous-value-of-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution:

Since you already have the expanded array, you can use map on the whole array and use a function to replace the values:

var weeks = [[2019/37,2],[2019/38,0],[2019/39,0],[2019/40,3],[2019/41,0],[2019/42,4],[2019/43,0],[2019/44,0]];
weeks.map((a,b)=>{weeks[b][1] = (a[1] == 0 && b > 0) ? weeks[b-1][1] : weeks[b][1]});

To make it more readable, this is the same as:

weeks.forEach(function missing(item,index,arr) {
    if (item[1] == 0 && index > 0) {
      arr[index][1] = arr[index-1][1];
    }
  }
);

Console log:

enter image description here

References:

Arrow Functions

Conditional Operator

Array.prototype.map()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...