0

I have array anArray containing this:

var entry1 = ['variable1','X'];
var entry2 = ['variable2','Y'];
var entry3 = ['variable3','Z'];
var anArray = [entry1, entry2, entry3];

Now I would like to do the following:

var variable1 = ...

where the ... is supposed to be a piece of magic code which searches anArray with the name of the variable and returns the value 'X' (so that variable1 ends up with the value 'X'). If the name of the variable was variable2 it should end up with the value 'Y' etc.

Is this possible?

If it is not possible to get it done in quite as magic of a fashion that I am wishing for: what is the most compact code to get the value 'X' into variable1, 'Y' into variable2 etc. ?

3 Answers 3

1

Update based on Tanaike's comment:

You can also try this:

let [variable1,variable2,variable3] = ["X","Y","Z"]

What you are describing as a magic code is not possible.

However, what I think it is the closest approach to what you are looking for is to create a json object or in other words key value pairs:

aJson = {
  variable1 : "X",
  variable2 : "Y",
  variable3 : "Z"
}

console.log(aJson.variable1) // output: X
console.log(aJson.variable2) // output: Y
console.log(aJson.variable3) // output: Z

The key will be the name of your variable and the value will be the value you want to assign to the specific key.

You can also pass the key as a string e.g. aJson['variable1']

Sign up to request clarification or add additional context in comments.

6 Comments

For example, I thought that when the destructuring assignment is used for your sample, it might be closed to OP's goal. How do you think about this?
@Tanaike thanks a lot for your comment. true I added it as an alternative. Let's see...
Thank you for quick reply. Yes. I thought that your additional sample might be closed to OP's goal for what is the most compact code to get the value 'X' into variable1, 'Y' into variable2 etc. ?. But I'm not sure whether that is the same direction OP expected. About this, I apologize.
@Tanaike thanks for your support. True, it might not be what he wants, but let's see if he can provide more details.
Thanks for the suggestions. I have never understood what these json objects are, but now I do know which is great. In this particular case it is not the approach that I want to follow, since the array anArray is actually more complex than described here and is already populated within a bigger code construct that I do not want to touch. Thus, since it seems as if the 'magic' version I had hoped for is not possible, my question is: : what is the most compact code to get the value 'X' out of anArray into variable1, 'Y' out of anArray into variable2 etc. ?
|
0

If you really don't want to use Objects

You could do something like this

var entry1 = ["variable1", "X"];
var entry2 = ["variable2", "Y"];
var entry3 = ["variable3", "Z"];
var anArray = [entry1, entry2, entry3];

// This is just a way to initialize the variable to a value it needs to find
var variable1 = "variable1";

// Go through anArray
for (let i = 0; i !== anArray.length; i++) {
  if (anArray[i][0] === variable1) {
    variable1 = anArray[i][1];
    break;
  }
}

console.log(variable1); // will give "X"

This of course assumes that the sub-lists will always have the same structure and length. i.e:

[
    ["name", "value"],
    ["name", "value"],  
    ["name", "value"],  
    ["name", "value"],  
    ["name", "value"],  
    ["name", "value"],
    ...
]

If you had one that was ["value", "name"] then this would not work.

In the way you want it to work:

// define a function
function getValue (variable, anArray){
  for (let i = 0; i !== anArray.length; i++) {
    if (anArray[i][0] === variable) {
      return anArray[i][1];
    }
  }
}

// define your variable with the value to search for
var variable1 = "variable1";

// call the function with the variable and the array
variable1 = getValue(variable1, anArray);

console.log(variable1);  // "X"

Comments

0

I solved it like this:

  let variable1 = anArray.filter(dataRow => dataRow[0] === 'variable1')[0][1];
  let variable2 = anArray.filter(dataRow => dataRow[0] === 'variable2')[0][1];
  let variable3 = ...

  console.log(variable1);  // "X"
  console.log(variable2);  // "Y"

I am not 100% sure I understand how this works, but it does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.