Currently i have a btn which calls a function as follows:
JavaScript
x
9
1
function ingredientsList() {
2
const allIngredients = [].concat(ingredientsResults.map(obj => obj.ingredients))
3
4
allIngredients.reduce((acc, item) => {
5
acc[item] = (acc[item] || 0) + 1
6
return (document.getElementById("resultsDiv").innerText = acc)
7
},{})
8
};
9
this gets information from a bunch of arrays as follows:
JavaScript
1
16
16
1
const ingredientsResults = [
2
{
3
dinnerName: "Vegetable Soup",
4
ingredients: ["potatoes", "onion", "spring onion", "lentils", "beans", "turnip" ]
5
},
6
{
7
dinnerName: "Spaghetti",
8
ingredients: ["spaghetti pasta", "tomato box","tomato box", "onion", "sundried tomatoes", "tomato paste", "lentils"]
9
},
10
{
11
dinnerName: "Fiskebolle",
12
ingredients: ["box of fiskebolle", "box of fiskebolle", "potatoes", "brocolli", "carrots"]
13
}
14
];
15
16
when the button is clicked it returns [Object, object] back to the “resultsDiv”.I have researched how to put this either in a list / table with the concatinated results but the only thing i find is JSON.stringify() and that gives me a bunch of nonsense! Is there a reason for this or am i missing something? i primarily want to display the results in a table / list
my desired results are something as follows:
JavaScript
1
15
15
1
{potatoes: 2, onion: 2, spring onion: 1, lentils: 2, beans: 1, …}
2
beans: 1
3
box of fiskebolle: 2
4
brocolli: 1
5
carrots: 1
6
lentils: 2
7
onion: 2
8
potatoes: 2
9
spaghetti pasta: 1
10
spring onion: 1
11
sundried tomatoes: 1
12
tomato box: 2
13
tomato paste: 1
14
turnip: 1
15
Any help is greatly appreciated!
Advertisement
Answer
Something you can do is after creating the object with the name of the ingredient and the quantity, it is creating a new loop going through all the objects and creating the tr
and td
JavaScript
1
44
44
1
const ingredientsResults = [
2
{
3
dinnerName: "Vegetable Soup",
4
ingredients: ["potatoes", "onion", "spring onion", "lentils", "beans", "turnip" ]
5
},
6
{
7
dinnerName: "Spaghetti",
8
ingredients: ["spaghetti pasta", "tomato box","tomato box", "onion", "sundried tomatoes", "tomato paste", "lentils"]
9
},
10
{
11
dinnerName: "Fiskebolle",
12
ingredients: ["box of fiskebolle", "box of fiskebolle", "potatoes", "brocolli", "carrots"]
13
}
14
];
15
16
17
function ingredientsList() {
18
const allIngredients = [].concat(ingredientsResults.map(obj => obj.ingredients))
19
20
const result = allIngredients.reduce((acc, item) => {
21
acc[item] = (acc[item] || 0) + 1
22
return acc // change this
23
},{})
24
// add this
25
const table = document.querySelector('#myTable')
26
27
Object.keys(result).forEach(dname => {
28
const tr = document.createElement('tr')
29
const td1 = document.createElement('td')
30
const td2 = document.createElement('td')
31
32
td1.innerText = dname
33
td2.innerText = result[dname]
34
35
tr.append(td1)
36
tr.append(td2)
37
38
table.append(tr)
39
})
40
41
// end
42
};
43
44
ingredientsList()
JavaScript
1
3
1
<table id="myTable">
2
3
</table>