I am currently writing a program that returns an response from a API using fetch.
As of now, I have the fetch call working. I then splice the array and use some regex to turn the single string response into a array of numbers ( while also replacing the /n with a , to format the data, because it was in raw CSV form ).
Here is the code for that...
let proxy = "https://cors-anywhere.herokuapp.com/"
let url = proxy + "https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hess"
function getPlayer() {
return fetch(url)
.then((response) => response.text())
.then((data) => {
console.log(data)
return data;
});
}
getPlayer().then((playerData) => {
console.log(playerData);
// This next line removed the indentation and then splits the response into an array of multiple numbers and pushes that array to the array playerStats.
playerStats.push(playerData.replace(/\n/ig, ',').split(','));
console.log(playerStats);
});
My issue is that this response returns 183 numbers, each representing one of the players stats.
I want to save each of these numbers as a variable, but doing let attackLevel = playerStats[0]
would be very tedious and will leave me with 183 variables.
My question is, what is the best way of going about saving each number as a variable?
My current thinking is that I have the array let playerStats = ["100", "242", "50"]
and somehow create variables by parallel linking to an array let statNames = ["attack", "defense", "age" ]
. My thinking is that doing something like this would allow me to just create a 2 arrays and let the js generate the variables.
I am not sure if this is a valid solution nor how to go about something like this.
Another option I see is creating an array of objects using both arrays. This way, instead of having 183 variables, I could just create key:pairs using the 2 arrays. I am thinking it would look something like this....
'let stats = [ attack: 100, defense: 242, etc ]`
going with this second solution would serve me better because I am using svelte and It would be a lot easier to work with an array of objects.
I am not sure what the best course of action is or how to go about creating these object key pairs using these 2 arrays.