var a = [
['2023-04-01', 0],
['2023-04-02', 0],
['2023-04-03', 3],
['2023-04-04', 0],
['2023-04-05', 5],
['2023-04-06', 0],
['2023-04-07', 0],
['2023-04-08', 0],
['2023-04-09', 0],
['2023-04-10', 10],
['2023-04-11', 0],
['2023-04-12', 0],
]
// 把数组中数组第二个值为0的替换为相邻不为0的值
function fn(a){
for(var i=0; i<a.length; i++) {
for(let j=i+1; j<a.length; j++){
if(a[j][1] !== 0 && a[i][1] == 0) {
a[i][1] = a[j][1]
}
}
for(let k=a.length-1; k>1; k--){
// console.log(a[k][1]);
if(a[k][1] !== 0 && a[i][1] == 0) {
a[i][1] = a[k][1]
}
}
}
}
fn(a)
console.log(a);
返回结果
[ [ '2023-04-01', 3 ],
[ '2023-04-02', 3 ],
[ '2023-04-03', 3 ],
[ '2023-04-04', 5 ],
[ '2023-04-05', 5 ],
[ '2023-04-06', 10 ],
[ '2023-04-07', 10 ],
[ '2023-04-08', 10 ],
[ '2023-04-09', 10 ],
[ '2023-04-10', 10 ],
[ '2023-04-11', 10 ],
[ '2023-04-12', 10 ]
]