English 中文(简体)
能够称呼这些价值观的短期变数价值—— Java本
原标题:Sort variables value while being able to call the values - JavaScript

我想将各种数值分为多种变量,仍然能够使用变数名称(价值1)

      var value1 = 4
      var value2 = 3
      var value3 = 5
      var value4 = 1
      var value5 = 2
      //This are the values that I want to sort//

我如何能够对这些变量进行分类,并且仍然能够使用这些变量的名称(价值1)加以称呼。

对这一局势的任何帮助将受到赞赏。

Can you also include an explanation of the code, I would be grateful.

问题回答

因此,如果我理解你的问题,这是令人感兴趣的。 但基本上,你可以这样做,把你们的变量提升为这样的目标:

var value1 = 4
var value2 = 3
var value3 = 5
var value4 = 1
var value5 = 2

const data = { value1, value2, value3, value4, value5 };

With that in place, they now have names! So you can use Object to help yourself out:

const values = Object.values(data).sort();
Object.keys(data).sort().map((k, idx) => {
    data[k] = values[idx]
});

这样一来,这些价值观和物体的名称就相应。 导致:

{
  value1: 1,
  value2: 2,
  value3: 3,
  value4: 4,
  value5: 5
}

So now you can call data.value1 to get the value1 out as you indicated in your question and get a value that has been sorted into it.

这对我来说就是一个严重的问题。 你实际上试图做些什么,因为对一个阵列进行分类,是一条角外的搜索,而不是对这个地点的适当问题。

You can sort your variables by putting them into an array an then using .sort() which sorts numbers by value.

let value1 = 4;
let value2 = 3;
let value3 = 5;
let value4 = 1;
let value5 = 2;
//Changed var to let (Better practice)

//Declaring an array. (Separate from value# vairable)
let values = [value1, value2, value3, value4, value5];

//Sorting right away so nothing is modified in future code.
values = values.sort();

//Logging results
console.log("Sorted values: " + values);

希望:

Edit:在阵列上,我为自己的价值确定了价值,但进行了分类。 将来的代码不修改原始变量。

我认为,你希望这样做。

左边的部分是将价值1......变量重新分配给右边的分类结果。

var value1 = 4;
var value2 = 3;
var value3 = 5;
var value4 = 1;
var value5 = 2;

[value1, value2, value3, value4, value5] = [value1, value2, value3, value4, value5].sort();

console.log(value1, value2, value3, value4, value5);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签