简体   繁体   English

For...of 循环调用字符串中的变量名

[英]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.对 JS 和一般代码来说非常新(不到一周,假设知识库为 0),所以我对问题的解释将缺乏光泽,但这里是纲要。

I have a function which takes the values of two obj.我有一个函数,它采用两个 obj 的值。 Arrays and adds the values together.数组并将值加在一起。 It then sends that sum to a new array (allinit), and finally sorts the new array numerically.然后将该总和发送到一个新数组 (allinit),最后按数字对新数组进行排序。 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:使用第二个函数(有趣)和 For...of 循环,我生成了以下内容:

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:我如何使用这个 For...of 函数来打印:

10 Bob Initiative

currently '10 initiative'目前“10个倡议”

by calling on the variable name that the element 10 represents?通过调用元素10代表的变量名称?

The immediate answer will be to use a obj/key Array.直接的答案是使用 obj/key 数组。 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.但是,据我所知,我无法对 obj/key 数组进行排序,并且必须在计算总和后对其进行数字排序。 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.我愿意接受建议或打印我的完整代码(约 50 行)作为上下文。

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()函数中可能有大约 6 个变量,每个变量都有自己的 sum 和 .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));

};

Without extra context, since "initiative" is making me think about RPGs, I'm going to assume this is for something like that.没有额外的背景,因为“主动性”让我想到 RPG,我假设这是为了类似的东西。 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!

Not entirely sure I understand what you are going for, but it sounds like you want to use Object.entries , then sort based on the number, then deal with that.不完全确定我明白你要做什么,但听起来你想使用Object.entries ,然后根据数字排序,然后处理它。

 var jim = 10 var Dave = 15 var Bob = 20 console.log( Object .entries({Bob, jim, Dave}) // [['Bob', 20], ['jim', 10], ['Dave', 15]] .sort((a, b) => a[1] - b[1]) // [['jim', 10], ['Dave', 15], ['Bob', 20]] .map((v) => `${v[0]} ${v[1]} initiative`) .join('\\n') );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM