Reputation: 31
I am trying to get a list of all the combinations of two array of objects.
I have two json files:
Fruits
[
{
"food": "Apple",
"calories": 100
}
],
[
{
"food": "Orange",
"calories": 150
}
]
Meat
[
{
"food": "Chicken",
"calories": 175
}
],
[
{
"food": "Steak",
"calories": 200
}
]
and I want to output all combinations of objects in the arrays:
Apple 100 Chicken 175
Apple 100 Steak 200
Orange 150 Chicken 175
Orange 150 Steak 200
I have a stupid simple for loop:
for(let i = 0; i < Fruits.length; i++) {
for(var j = 0; j < Meat.length; j++)
{
combos.push(Fruits[i] + Meat[j])
}
};
that is ending up giving me:
[
'[object Object][object Object]',
'[object Object][object Object]',
'[object Object][object Object]',
'[object Object][object Object]'
]
If i do a map i get to it a little more
combos.map(combo=> {console.log(combo)})
gets
[object Object][object Object]
[object Object][object Object]
[object Object][object Object]
[object Object][object Object]
But I'm not sure how to get at the objects or even if they are defined there.
Thanks for reading!
Edited: With the addition of some more indexes and brackets I was able to get what I needed with this:
for(let i = 0; i < this.arrBreakfasts.length; i++) {
for(var j = 0; j < this.arrLunches.length; j++){
this.combos.push([this.arrBreakfasts[i][0], this.arrLunches[j][0]]);
}
};
Thanks!!
Upvotes: 2
Views: 107
Reputation: 4519
You could use a for loop
meals=[[ { "food": "Apple", "calories": 100 } ], [ { "food": "Orange", "calories": 150 } ]]
fruits=[[ { "food": "Chicken", "calories": 175 } ], [ { "food": "Steak", "calories": 200 } ]]
res=[]
for(let i = 0 ;i < meals.length; i++){
for(let j = 0;j <fruits.length; j++){
b=[meals[i][0].food,meals[i][0].calories,fruits[j][0].food,fruits[j][0].calories]
res.push(b)
}
}
console.log(res)
Upvotes: 0
Reputation: 89517
You need to concatenate the properties of the objects instead of the objects themselves.
for(let i = 0; i < Fruits.length; i++) {
for(var j = 0; j < Meat.length; j++){
combos.push(Fruits[i][0].food + " " + Fruits[i][0].calories + " " + Meat[j][0].food + " " + Meat[j][0].calories);
}
};
Demo:
const Fruits = [
[
{
"food": "Apple",
"calories": 100
}
],
[
{
"food": "Orange",
"calories": 150
}
]
];
const Meat = [
[
{
"food": "Chicken",
"calories": 175
}
],
[
{
"food": "Steak",
"calories": 200
}
]
];
let combos = [];
for(let i = 0; i < Fruits.length; i++) {
for(var j = 0; j < Meat.length; j++){
combos.push(Fruits[i][0].food + " " + Fruits[i][0].calories + " " + Meat[j][0].food + " " + Meat[j][0].calories);
}
};
console.log(combos);
Upvotes: 0
Reputation: 4942
const fruits = [
[{ food: 'Apple', calories: 100 }],
[{ food: 'Orange', calories: 150 }]
];
const meat = [
[{ food: "Chicken", calories: 175 }],
[{ food: "Steak", calories: 200 }]
];
const combos = [];
fruits.forEach(fruit => meat.forEach(meat => combos.push([fruit, meat])));
combos.forEach(combo => console.log(JSON.stringify(combo, null)));
Upvotes: 0
Reputation: 1736
You are supposed to do it like this:
for(let i = 0; i < Fruits.length; i++) {
for(var j = 0; j < Meat.length; j++)
{
combos.push([Fruits[i], Meat[j]])
}
};
What you are doing there is trying to do a math operation with objects.
Upvotes: 2