I have an API call that returns an Object that contains a nested Array of Objects.
I want to add a property to all the Objects in Key3's array and set those values individually from another API call but i cant get it to work...
var object = {
key1: "string",
key2: bool,
key3: [
{ key1: "Thing1", key2: value2, key3: value3 },
{ key1: "Thing2", key2: value2, key3: value3 },
{ key1: "Thing3", key2: value2, key3: value3 },
{ key1: "Thing4", key2: value2, key3: value3 },
{ key1: "Thing5", key2: value2, key3: value3 }
]
}
var array = object.key3;
for (var i = 0; i < array.length; i++) {
axios.get(`https://api.callblahblablah${url[array[i].key1]}`)
.then(response => {
array.forEach(function(obj) { obj.key4 = response.data.result; });
//response.data.result is a float.
this.setState({data: array})
})
.catch(err => console.log(err));
}
}
The API call returns a specific value (float) that corresponds with a particular object, I want to match them up.
At the moment Key4 is added to each Object in array ok but its value is the same for every object.
I'm trying to match the correct value returned by each api call to its corresponding object. So the first api call adds the key-value pair returned to array[0], the second api call adds key4 and it's value array[1], the third api call adds key4 and the value returned to array[2] and so on and so forth.
I know it's something to do with the forEach.