I'm attempting to iterate through a JSON file, retrieving the calorie count for each item and then sorting by meal and day (in total, there are only five days of data). Here is an example of an object in the array.
"Date": "Thursday",
"Name": "Shredded Chicken",
"Icon": "Chicken",
"Type": "Dinner",
"Quantity": 85,
"Units": "Grams",
"Calories": 90
Currently, the totals for breakfastCalories
, lunchCalories
, dinnerCalories
, and snackCalories
are being added together (i.e. Monday's breakfastCalories is being added to Tuesday's breakfastCalories, and so on).
GOAL The intended result is to have an array for each day, listing the total breakfastCalories, et al. for that day. This only worked for day 1, print(calTotal[18])
returns [465,380,530,153,1528,899]
, which is the correct array of values for breakfastCalories, lunchCalories, dinnerCalories, snackCalories, grossCalories, netCalories
.
let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]; //currently does nothing
let calTotal = [];
function setup() {
createCanvas(windowWidth, windowHeight);
loadJSON("finalJSON.json", cbSuccess, cbFail);
}
function draw() {
//to be utilized
}
function cbFail(data) {
print("Error loading JSON File");
print(data);
}
function cbSuccess(data) {
let myData = data; //load our JSON array
let calories = 0; //set a 0 starting value for calories
let breakfastCalories = 0;
let lunchCalories = 0;
let dinnerCalories = 0;
let snackCalories = 0;
let burnedCalories = 0;
for (let i = 0; i < myData.length; i ) { //count number of total entries in the dataset
if (myData[i].Type == "Breakfast") {
breakfastCalories = myData[i].Calories;
}
else if (myData[i].Type == "Lunch") {
lunchCalories = myData[i].Calories;
}
else if (myData[i].Type == "Dinner") {
dinnerCalories = myData[i].Calories;
}
else if (myData[i].Type == "Snacks") {
snackCalories = myData[i].Calories;
}
else if (myData[i].Type == "Exercise") {
burnedCalories = myData[i].Calories;
}
let grossCalories = round(breakfastCalories lunchCalories dinnerCalories snackCalories);
let netCalories = round(breakfastCalories lunchCalories dinnerCalories snackCalories - burnedCalories);
calTotal.push([round(breakfastCalories), round(lunchCalories), round(dinnerCalories), round(snackCalories), grossCalories, netCalories]);
}
}
More Concise Example (complete JSON data can be found here)
Example JSON
...
{
"Date": "Monday",
"Name": "Wild Pacific Sardines",
"Icon": "Fish",
"Type": "Lunch",
"Quantity": 85,
"Units": "Grams",
"Calories": 170
},
...
{
"Date": "Monday",
"Name": "Bacon, Sunday, Organic, Uncured",
"Icon": "Bacon",
"Type": "Dinner",
"Quantity": 1,
"Units": "Slice",
"Calories": 30
},
...
{
"Date": "Tuesday",
"Name": "Dressing, Vinaigrette, Lemon Pepper",
"Icon": "Oil",
"Type": "Lunch",
"Quantity": 2,
"Units": "Tablespoons",
"Calories": 200
},
For each day (Monday through Friday), add all values
of Calories
in Type == Breakfast
together, all values ofCalories
in Type == Lunch
, etc, and at the end of the day, present calorie totals in an array.
CodePudding user response:
You can use this code:
const data = fetch('https://api.npoint.io/0b9f68319d190f5d41ec')
.then(result => result.json())
.then((output) => {
return output;
}).catch(err => console.error(err));
data.then(items=>{
let result ={};
items.map(item=>{
let Date = item.Date;
let Type = item.Type;
if(result.hasOwnProperty(Date) && result[Date].hasOwnProperty(Type)){
result[Date][Type] = item.Calories;
}else if(result.hasOwnProperty(Date)){
result[Date][Type] = item.Calories;
}else{
result[Date]={};
result[Date][Type] = item.Calories;
}
if(Object.keys(result[Date]).length == 5){ //each day have 5 prop (Breakfast,Lunch,.)
result[Date]['netCalories'] = result[Date]['Breakfast'] result[Date]['Lunch'] result[Date]['Dinner'] result[Date]['Snacks'] - result[Date].Exercise;
result[Date]['grossCalories'] = result[Date]['Breakfast'] result[Date]['Lunch'] result[Date]['Dinner'] result[Date]['Snacks'];
}
});
console.log(result);
});