0

I'm brand new to javascript and appreciate everyone's help. I'm looping an array that might have 5 to 10 different records in it. This is what I'm doing so far and it works just fine. I didn't think including the array was necessary but let me know if it is.

obj = relatedActivities.data;
console.log(obj);

  for (var i = 0; i < obj.length; i++) {
     var activityType = (obj[i].Activity_Type)
    }

The only problem with this is I need to put each record's value in a particular place. What I want is a different variable every time it loops.

So the first record, the variable name would be something like:

activityType0 = obj[0].Activity_Type

and for the second record it would be:

activityType1 = obj[1].Activity_Type

I hope that makes sense.

Thank you all much!

  • 3
    Why not put the values in an array? Why do you feel that you need separate variables? That's not a typical thing to do in JavaScript. – Pointy Oct 24 '22 at 14:06
  • What is the intended use for this? Is there a reason you can't just call the corresponding value in the current existing array? If your variables are `activityType0` and `activityType1` then somewhere you are referencing the **0** and **1** values to call those specific variables. Why not just use `relatedActivities.data[0].Activity_Type`? It may seem more efficient because the variable names are easier to read or shorter, but it is far less efficient. – EssXTee Oct 24 '22 at 14:14

2 Answers2

1

While I'm not sure there is any practical use for doing this, I will post this for the sake of answering the question.

Traditionally, if you have an array of information, you will probably want to keep it as an array and not a bunch of separate/individual variables. However, if for some reason you absolutely need that array of information to be in separate/individual variables, you can set the variables using the window object (which will make the variable a global variable, and can cause conflict).

relatedActivities.data.forEach((obj, i) => {
    window[`activityType${i}`] = obj.Activity_Type
});

console.log(activityType0);
console.log(activityType1);

Basically any global variable is typically called by its variable name, like activityType0. However, you can also call it through the window object like so: window.activityType0 or window["activityType0"]. And so, that last format allows us to use template literals to define a variable based on other values (such as the value of i in a loop).

EssXTee
  • 1,783
  • 1
  • 13
  • 18
  • Thank you much. This was very helpful. I definitely need to get to the basics of Javascript and I will soon to learn best practices. The data for each record is mixed. I outlined it in more depth [HERE](https://stackoverflow.com/questions/74176683/javascript-best-practices-sorting-mixed-data-from-array) in this post. The reason I was pulling the data into variables is because they need to be modified separately. But, I'll take the advice of using the information from the array and see what I can do. I appreciate it. – Nathan Zoho Oct 24 '22 at 14:45
1

Well | maybe you hope so,This is basically the same answer as above | except that we avoid namespace pollution

relatedActivities.data.forEach((o, i,arr) => {
   arr[i] = {}; 
   arr[i][`activityType${i}`] = o.activityType;  
})

relatedActivities.data.forEach(o => console.log(o))