0

I have a big document that shows nutrition data of a dummy recipe.

Look at the codesandbox json first

i want to get total calories and nutrition facts of this object;

recipe has an array of stuffs and in stuffs array we have 2 item objectes;

each item has fact ( facts );

facts are ( vitamins, nutrition, calories and others )

each fact has value;

I tried many nested array.map high order function, for example i got calories of each item, but problem was that nested map function returns each calorie value in a seperate array...

i want to push values of each item(recipe stuff item) in arrays like this:

const vitaminValues = []
const nutritionValues = []
const caloriesValues = []
const othersValues = []

if i get this, i can get total calories, vitamins, nutrition and others information

but idk, if u guys have any better ideas just help me :) thanks

this is JSON file of sample recipe : https://codesandbox.io/s/relaxed-brown-zrxnr?fontsize=14&hidenavigation=1&theme=dark

6
  • 1
    flatMap is your friend. I won't say more until you provide your earnest attempt Commented May 2, 2020 at 6:55
  • please mention what output you want from this Commented May 2, 2020 at 8:39
  • @SohanPatil, as I said, I want to calculate and get sum of calories, nutrition, vitamins and others of all and each stuff in a recipe Commented May 2, 2020 at 8:49
  • can you pleae provide sample output Commented May 2, 2020 at 8:51
  • @SohanPatil , I said Commented May 2, 2020 at 8:53

1 Answer 1

1
let data=//your json
let vitaminValues = [];
let nutritionValues = [];
let caloriesValues = [];
let othersValues = [];
let vitaminAValues = [];

function getValuesFromJson(obj, Arr) {
  Object.entries(obj).map(
    vitamin => vitamin[1].hasOwnProperty("value") && Arr.push(vitamin[1].value)
  );
}
data.map(d =>
  d.stuffs.map(stuff =>
    stuff.item.fact.map(f => {
      getValuesFromJson(f.vitamins, vitaminValues);
      getValuesFromJson(f.others, othersValues);
      caloriesValues.push(f.calories);
      f.nutritions.map(nutri => getValuesFromJson(nutri, nutritionValues));
      vitaminAValues.push(f.vitamins.A.value);
    })
  )
);
console.log("vitaminValues", vitaminValues);
console.log("nutritionValues", nutritionValues);
console.log("othersValues", othersValues);
console.log("caloriesValues", caloriesValues);
console.log("vitaminAValues", vitaminAValues);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it works well! <3 What if i want all other childs? for example :
vitaminA_values = [] vitaminC_values = [] vitaminD_values = [] vitaminB6_values = [] calcium_values = [] iron_values = [] cobalamin_values = [] magnesium_values = []
fat_values = [] cholesterol_values = [] sodium_values = [] potassium_values = [] carbohydrate_values = [] protein_values = []
@tafhim i have edited the code for adding values to vitaminAValues array you can repeat the same for other vitamins

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.