2

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!!

2
  • 1
    Is there any reason to wrap each of those objects into array (of single item)? Commented Jul 25, 2020 at 21:26
  • I had the "fruit" as a sheet, and the "meat" as another sheet in google docs which I downloaded as csv and converted to json. This was the format the converter gave to me. Commented Jul 25, 2020 at 22:02

4 Answers 4

2

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.

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

Comments

0

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)));

Comments

0

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);

Comments

0

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)

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.