1

Please help me to do this!

I'm getting data array from database by month. The problem I'm having is that I'm fetching data like this

[{January: 6}, {February: 2}, {October: 7}, {December: 4}, {November: 5374}]

wherever there is null I don't even get any value which causes problem for me to work so I want data like this

[{January: 6}, {February: 2}, {March: 0}, {April: 0}, {May: 0}, {June: 0}, {July: 0}, {August: 0}, {September: 0}, {October: 7}, {November: 5373}, {December: 4}]

but data is fetched dynamically from database which is giving me only values which are more than 0. I just want to get all months with default values as 0.

1
  • 1
    This is not enough info to help you. What database? How do yo call it (what query)? Also, are all months actually present in the database with the value null (or 0)? Or absent altogether? With the current information, it's impossible to say for certain why nullish entries get skipped. Commented Mar 22, 2021 at 8:42

4 Answers 4

2

You could build an object and take an array of minth names for creating an array of objects.

const
    year = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'September', 'October', 'November', 'December'],
    data = [{ January: 6 }, { February: 2 }, { October: 7 }, { December: 4 }, { November: 5374 }],
    object = Object.assign({}, ...data),
    result = year.map(month => ({ [month]: object[month] || 0 }));

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

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

Comments

1

You can get all months using Date.toLocaleString() and array#from and then assign 0 to missing month.

const input = [{January: 6}, {February: 2}, {October: 7}, {December: 4}, {November: 5374}],
      allMonths = Array.from({length: 12}, 
              (_,i) => new Date(2020,i).toLocaleString({}, {month: 'long'})),
      lookup = Object.assign(...input),
      result = allMonths.map(month => ({ [month]: lookup[month] || 0 }));

console.log(result);

Comments

0

You can specify a default list in your program, and if you don't get the data from the database you then refere back to the default list.

let defaults = [{January: 0}, {February: 0}, {March: 0}, {April: 0}, {May: 0}, {June: 0}, {July: 0}, {August: 0}, {September: 0}, {October: 0}, {November: 0}, {December: 0}];

let databaseValue = [{January: 6}, {February: 2}, {October: 7}, {December: 4}, {November: 5374}];

let monthToSearch = "June";
let databaseFound = databaseValue.find(o => o.hasOwnProperty(monthToSearch));
if (!databaseFound){
  databaseFound = defaults.find(o => o.hasOwnProperty(monthToSearch));
}

console.log(databaseFound);

Comments

0

Here you go! You can simply transform the fetched data and update that object by iterating over it.

var data = [{January: 6}, {February: 2}, {October: 7}, {December: 4}, {November: 5374}];

var theMonths = ["January", "February", "March", "April", "May",
  "June", "July", "August", "September", "October", "November", "December"];

var monthsfetched = data.map(x =>  Object.keys(x)[0]);

theMonths.map(mon => {
  if(!monthsfetched.includes(mon)){  
    let missing_entry = {};
    missing_entry[mon] = 0;
    data.push(missing_entry);
  }
})

console.log(data)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.