3

I have two arrays:

  1. category.data.xAxis // containing: ["0006", "0007", "0009", "0011", "0301"]
  2. category.data.yAxis // containing: [6.31412, 42.4245, 533.2234, 2345.5413, 3215.24]

How do I take a max length, say DATAPOINT_LENGTH_STANDARD = 540 and fill in every missing number-based string on the xAxis array? so that:

  1. The resulting xAxis array will read from "0000" to "0540" (or whatever the standard length is)
  2. The associated yAxis indexes will remain connected to the original xAxis datapoints (i.e. "0006" to 6.31412)
  3. Every newly created xAxis datapoint has an associated yAxis value of 0 (so the newly created "0000" xAxis entry will contain 0 in the yAxis at index 0 for both)

The xAxis value strings can be assumed to already be sorted from lowest to highest.

EDIT

The clarity of my question was an effort to help the community with finding a more straight-forward answer, but I suppose the exactness gave the appearance of a homework question. In response to the comments, my original attempt, which does not properly manipulate the x-axis and maintain proper indexing:

let tempArray = categoryObject.data.xAxis;
let min = Math.min.apply(null, tempArray);
let max = Math.max.apply(null, tempArray);
while (min <= max) {
  if (tempArray.indexOf(min.toString()) === -1) {
      tempArray.push(min.toString());
      categoryObject.data.yAxis.push(0);
  }
  min++;
}
console.log(tempArray);
console.log(categoryObject.data.yAxis);
1
  • It's not a homework problem, I'll update my post with some of my attempts that don't quite get what I'm looking for. Commented Jun 24, 2019 at 22:05

1 Answer 1

4

let xAxis = ["0006", "0007", "0009", "0011", "0301"]
let yAxis = [6.31412, 42.4245, 533.2234, 2345.5413, 3215.24]
const DATAPOINT_LENGTH_STANDARD = 540

// This assumes at least one data point
let paddedLength = xAxis[0].length
// Creates a new array whose first index is 0, last index is
// DATAPOINT_LENGTH_STANDARD, filled with 0s.
let yAxis_new = new Array(DATAPOINT_LENGTH_STANDARD + 1).fill(0)
// Copy each known data point into the new array at the given index.
// The + before x parses the zero-padded string into an actual number.
xAxis.forEach((x, i) => yAxis_new[+x] = yAxis[i])
// Replace the given array with the new array.
yAxis = yAxis_new
// Store the padded version of the index at each index.
for (let i = 0; i <= DATAPOINT_LENGTH_STANDARD; ++i) {
  xAxis[i] = ('' + i).padStart(paddedLength, '0')
}

console.log(xAxis)
console.log(yAxis)

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

2 Comments

This is indeed the correct code and I am unsure why you were downvoted. The comments and votes to close seem a tad hostile. My assumption is either someone did not feel you should assist me, or your post did not contain any explanations. It is probably the latter, so I would recommend adding some comments explaining the important lines. I will accept this answer if you add said comments, appreciate the help.
I added the requested 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.