1

I have got a data sample:

{
  data: [
    { day: 0, amount: 0 },
    { day: 3, amount: 5 },
    { day: 7, amount: 8 },
    { day: 8, amount: 14 }
  ]
}

For doing some math and calculating local regressions I need to fill up each missing day, while I know the total amount of days.

Lets say there are 10 days with the example above, this should be my outcome:

{
  data: [
    { day: 0, amount: 0 },
    { day: 1, amount: 0 },
    { day: 2, amount: 0 },
    { day: 3, amount: 5 },
    { day: 4, amount: 5 },
    { day: 5, amount: 5 },
    { day: 6, amount: 5 },
    { day: 7, amount: 8 },
    { day: 8, amount: 14 }
    { day: 9, amount: 14 }
  ]
}

How can I achieve this in style? (optional with ES6 syntax)


My own guess seems to be quite complicated:

First I could create an array with each day:

const amountOfTotalDays = 10;
const daysByAmount = [
  { day: 0, amount: 0 },
  { day: 3, amount: 5 },
  { day: 7, amount: 8 },
  { day: 8, amount: 14 }
];
const daysHelper = [...Array(amountOfTotalDays).keys()];

Then I could loop over each entry and check for the correct amount. Maybe like this:


    const daysByAmountUpfilled = [];
    daysHelper.map(d => {
      const existingDay = daysByAmount.findIndex(entry => entry.day === d);
      if(existingDay >= 0) {
        daysByAmountUpfilled.push(
          daysByAmount[existingDay]
        );
      } else {
        // find previous entry closest to this day
        // but I do not know how
      }
    });
3
  • 3
    Have you made any attempts that you can share with us? Commented Nov 19, 2019 at 16:07
  • You could consider trying a for loop and matching on the array index. Commented Nov 19, 2019 at 16:13
  • I have added my own attempt to the question. Commented Nov 19, 2019 at 16:16

3 Answers 3

4

You could mutate the array and insert the missing parts.

function fill(array, size) {
    var i;
    for (i = 0; i < size; i++) {
        if (array[i] && array[i].day === i) continue;
        array.splice(i, 0, Object.assign({}, array[i - 1], { day: i }));
    }
}

var data = { data: [{ day: 0, amount: 0 }, { day: 3, amount: 5 }, { day: 7, amount: 8 }, { day: 8, amount: 14 } ]};

fill(data.data, 10);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

One option is to create a new array of your desired length, and use .map() to fill it.

const days = 10;
const data = { data: [ { day: 0, amount: 0 }, { day: 3, amount: 5 }, { day: 7, amount: 8 }, { day: 8, amount: 14 } ] };

const result = (() => {
  let previousAmount = 0;
  return Array.from(Array(days)).map((item,idx) => {
    const existingDay = data.data.find(day => day.day === idx);
    previousAmount = (existingDay || {amount:previousAmount}).amount;
    return existingDay || { day: idx, amount: previousAmount }; 
})})();

console.log(result);

Comments

1

You can create a new array and fill in the missing days when appropriate:

const expectedDays = 10;
const result = {
  data: [
    { day: 0, amount: 0 },
    { day: 3, amount: 5 },
    { day: 7, amount: 8 },
    { day: 8, amount: 14 }
  ]
};
const fixedResult = {};

fixedResult.data = result.data.reduce((acc, v, i) => {
  const next = result.data[i + 1];
  const missing = next ? next.day : expectedDays;
  const fill = [];
  
  for (let i = v.day; i < missing; i++) {
    fill.push({ day: i, amount: v.amount });
  }
  
  return [...acc, ...fill];
}, []);


console.log(fixedResult);

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.