Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var input = [
 {x: Date(1234), y: 0},
 {x: Date(1235), y: 0},
 {x: Date(1236), y: 300},
 {x: Date(1237), y: 300},
 {x: Date(1238), y: 300},
 {x: Date(1239), y: 300},
 {x: Date(1240), y: 300},
 {x: Date(1241), y: 0},
 {x: Date(1242), y: 0},
 {x: Date(1243), y: 0},
];
var output = input.reduce(function (acc, val) {
  var lastIndex = acc.length - 1;
  if (val.y <= 0 || lastIndex < 0 || acc[lastIndex].y <= 0) {
    acc.push(val);
  } else {
    acc[lastIndex].y += val.y;
  }
  return acc;
}, []);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers