Search code examples
javascriptarraysfor-of-loop

For...of loop Which Calls the Variable Name in a String


Very new to JS and code in general (Less than a week, assume 0 knowledge base), so my explanation of the problem will be lack luster, but here's the rundown.

I have a function which takes the values of two obj. Arrays and adds the values together. It then sends that sum to a new array (allinit), and finally sorts the new array numerically. I need to keep them in numerical order and apply a unique string to each variable.

Using a second function (fun) and For...of loop I've generated the following:

var allinit = [jim,dave,bob]

var fun = (allinit) => {

for (var element of allinit) {
    console.log( element + [the name of the variable] + 'initiative')
}

Using the first function I have assigned:

var jim = 10  
var Dave =15   
var Bob = 20

How can I use this For...of function to print:

10 Bob Initiative

currently '10 initiative'

by calling on the variable name that the element 10 represents?

The immediate answer will be to use a obj/key Array. However, as far as I know I cannot sort an obj/key Array and it MUST be sorted numerically after the sums have been calculated. I'm having a hard time keeping things fluid enough maintain the correct order while still being able to name the variables each number represents. I'm open to suggestions or printing my full code (~50 lines) for context.

EDIT

Here is some shorthand of my code for context, keep in mind that this has many many lines removed from both arrays and the function. Theres probably about 6 variables in the function each with their own sum and .push()

var R = {
  a:5,
  d:2
};

var B = {
  a:10,
  d:18,
};

var Start = (R,B) => {
  var allinit = [

  ];

  var ai = (R.a + B.a);
  allinit.push(ai);

  var di = (R.d + B.d);
  allinit.push(di);

  allinit.sort((a,b)=>a-b);


  var fun = (allinit) => {

    for (var element of allinit) {
      console.log( elemement+ ** Variable name here** + ' inititive')
      }
  };

  console.log(fun(allinit));

};

Solution

  • Without extra context, since "initiative" is making me think about RPGs, I'm going to assume this is for something like that. I might be off-base here, but let's hope I'm not!

    The idea here is that each player, currently their name and initiative value (but extensible for more things) is represented by an {} object, and they're in an [] array.

    As you can see, I've deliberately ordered them in an arbitrary order, just so we can see sorting works as it should.

    var players = [
      {name: "Jim", initiative: 10},
      {name: "Bob", initiative: 20},
      {name: "Dave", initiative: 15},
      {name: "Uthorr the Star-Slayer", initiative: 3},
    ];
    
    // Declare a callback for sort - named here for clarity.
    function sortByInitiative(p1, p2) {
      // Sort into ascending initiative order (switch up `p1` and `p2` for descending).
      return p1.initiative - p2.initiative;
    }
    
    // Sort players in place.
    players.sort(sortByInitiative);
    
    // Loop over them and print salient information.
    players.forEach((player, index) => {
      console.log(`Player ${index + 1} is ${player.name} with initiative ${player.initiative}!`);
    });
    

    The output is

    Player 1 is Uthorr the Star-Slayer with initiative 3!
    Player 2 is Jim with initiative 10!
    Player 3 is Dave with initiative 15!
    Player 4 is Bob with initiative 20!