Which algorithm to downsample a 2D array of values to save space in js

I’m working with 1D and 2D arrays containing values for plotting heatmaps.
Some datasets can containing thousands of values which represent millions of values in the matrix. I’d like to try some downsampling/compression methods with some parameters to save space on disk and still be able to plot everything.

I have 3 arrays, X and Y being 1D and Z being 2D (of dimension X*Y).

I already have a downsampling method, which basically removes every k-th value, it works on huge datasets but I could lose some valuable information without any smart processing.