2

Given a list of values for charting like the below arrays

var arrays = [["A", [
                      [1391032800000, 20],
                      [1389826800000, 4],
                      [1389913200000, 4],
                      [1390086000000, 6]
                    ]
                ]];              
var dates = arrays[0][1].sort(function(x, y) { return x[0] - y[0]; });
var map = dates.map(function(dt) { return [new Date(dt[0]), dt[1]]; });
console.log(map);

The map contains:

enter image description here

I need to add additional Array(2) values into the map variable for all the days that are missing between Jan 16 and the end date of Jan 30.

What's the quickest way to fill the missing zero values (for each missing day) in the array.

5
  • Are the times of day potentially all different? Commented Jun 14, 2018 at 12:24
  • Where do the second values in each array(2) come from? i.e., the 4, 4, 6, 20 values Commented Jun 14, 2018 at 12:27
  • @Darren: No, the times of the day should all be the same, the time component should be ignored Commented Jun 14, 2018 at 12:30
  • @David The second values are a given, I have values for 4 particular days. I want to add zeros for all the dates (days) that are not represented. Commented Jun 14, 2018 at 12:31
  • 1
    I'd do it the opposite of what you're thinking - make the array of dates first (get first and last from your array then fill in the blanks) then loop through that array and map with values in arrays Commented Jun 14, 2018 at 12:32

2 Answers 2

1

You could use getDate method and then reduce method to create new array and get difference between each two dates and fill that difference with zeros.

var arrays = [
  ["A", [
    [1391032800000, 20],
    [1389826800000, 4],
    [1389913200000, 4],
    [1390086000000, 6]
  ]]
];
var dates = arrays[0][1].sort(function(x, y) {
  return x[0] - y[0];
});
var map = dates.map(function(dt) {
  return [new Date(dt[0]), dt[1]];
});

var result = map.reduce((r, [e], i, arr) => {
  if (i != 0) {
    let date = e.getDate();
    let prev = arr[i - 1][0].getDate();
    r.push(...Array(date - prev - 1).fill(0))
  }
  r.push([e]);
  return r;
}, [])


console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

this solution, whilst it looks great, if you run it, it doesn't show the dates for the missing entries, and it looses the values.
You said you want to insert 0 for missing days.
0

Assuming the time of the day is the same and based on Darren Sweeney's suggestion:

const data = [
    [1391032800000, 20],
    [1389826800000, 4]
];

// getting min and max dates in the array
const timestamps = data.map(([timestamp]) => timestamp);
const [min, max] = [Math.min(...timestamps), Math.max(...timestamps)];

// creating hash where evrey day in the range between min and max dates has a value of 0
let dataObj = {};
let tempTimestamp = min;

while (tempTimestamp <= max) {
    dataObj[tempTimestamp] = 0;
    tempTimestamp += 86400000;
}

// lopping through the original array and populating a "zero-valued" hash with values from original array
data.forEach(([timestamp, value]) => {
    dataObj[timestamp] = value;
})

// converting a hash into array
const result = Object.keys(dataObj).map(timestamp => ([timestamp, dataObj[timestamp]]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.