-1

I have an array valueArray[] with a length of 8, I want to take each index value of the array and assign it to a variable and achieve this -

var valueArray = ["itemName", "itemType"]

 var name = valueArray[index 1];
var type = valueArray[index 2];

myFunction(name,type)

Would anyone know how to get this? Loops don't work as the same variable will only change its value.

CodingNoob
  • 65
  • 6
  • 3
    `var name = valueArray[0]` and `var type = valueArray[1]` ? – derpirscher Feb 11 '21 at 14:00
  • Thanks for the reply but that doesnt work when the array content is dynamic – CodingNoob Feb 11 '21 at 14:00
  • 1
    If you don't know how many elements your array has, how would you define the correct number of variables? Or what do you mean by "dynamic" content? – derpirscher Feb 11 '21 at 14:02
  • The main point is obviously looping thru the array... the question is how do I assign the variables within the loop – CodingNoob Feb 11 '21 at 14:03
  • Does this answer your question? [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Lety Feb 11 '21 at 14:05
  • Do you know which element is located at what position? If yes, just access the element via index. If not, it can't done ... – derpirscher Feb 11 '21 at 14:05
  • This is not the right way to do something. what do you want to achieve with those variables? – Sachin Feb 11 '21 at 14:06
  • 1
    could you please add a proper format of the array to make it easy for others to understand? – kunal panchal Feb 11 '21 at 14:06
  • First, you've stated in the question that the array length is 8, so you need 8 variables. Second, in the code snippet you provided you've assigned the first element to name and the second to type, so you know which value to assign to each variable. So what is that you're trying to achieve that simply `var name = valueArray[0]` etc. cannot? – AHMED SAJJAD Feb 11 '21 at 14:15

2 Answers2

0

I'm unclear what you're trying to do, but to set a variable dynamically in JS the easiest way is to assign it to the window object:

window["variable_name"] = "Value";
//Now you can use variable_name as a global variable.
Explosion
  • 328
  • 2
  • 10
  • @Wale `window` is not an array but an object. And you can access an object's properties either via `objectname.propertyname` or `objectname["propertyname"]` So yes, that's perfectly valid syntax (eventhough probably not an answer to the question) – derpirscher Feb 11 '21 at 14:09
0

Not clear about what exactly you want to achieve. Try this.

for (let i = 0; i < array.length; i += 2) {
  let name = array[i];
  let type = array[i + 1];
}
Farrukh Normuradov
  • 1,032
  • 1
  • 11
  • 32
  • Hey thansk for the reply, i tried this but it skips to the last index for the second variable – CodingNoob Feb 11 '21 at 14:12
  • let arr = ["one ","Type-1", "two", "Type-2", "three", "type-3", "four", "type-4"] for(let i=0; i< arr.length; i +=2){ let name = arr[i]; let type = arr[i+1]; console.log(name, type) } // Logs one Type-1 two Type-2 three type-3 four type-4 – FAHEEM KHAN Feb 11 '21 at 14:40