Currently, I am facing a challenge in my project where I need to fetch data from an open-source API. The issue lies in the fact that the API provides objects that must be grouped into an array named ingredients
. These objects are labeled as
strIngredients1, strIngredients2, strIngredients3
, and so on. I am looking for a way to achieve this by utilizing a for loop, but I am unsure of how to proceed.
As of now, I have not attempted any solutions because I am uncertain about the approach to take.
EDIT: To clarify further, let's assume there are three objects named obj1
, obj2
, and obj3
. My goal is to add all of them to a single array called arr
. Typically, this would involve manually inserting each object into the array:
// Manual method
var arr = []
var obj1 = {}
var obj2 = {}
var obj3 = {}
arr.push(obj1, obj2, obj3)
However, I aim to accomplish this using a for loop instead. Somewhat similar to the following:
// For Loop
for (let i = 1; i <= 20; i++) {
arr.push('obj'+i)
}
Unfortunately, this approach only adds strings, not the actual objects. What steps should I take next?