Dynamisch auf bestimmtes Array zugreifen


Status
Dieses Thema wurde gelöst! Zur Lösung gehen…

Kai-Behncke

Erfahrenes Mitglied
#1
Hallo liebe Leute,
ich stehe vor folgender Herausforderung:

In einer Anwendung habe ich ca. 200 Arrays ungefähr in dieser Form:

Javascript:
var rh_smwbs_1=["1","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
var rh_smwbs_2=["3","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
var rh_smwbs_3=["3","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
Ich möchte nun, dass abhängig von zwei Formulareingaben auf ein bestimmtes Array zugegriffen wird.
Der Array-Name (also die Array-Variable) soll abhängig von den Formular-Werten aufgerufen werden.

Ich habe folgendes versucht:

Code:
onClick('wdform_16_element8', function() {

//Hier wird zunächst der String (z.B.) "rh_smwbs" geholt
var saatgutauswahl=document.getElementById('wdform_16_element8').value;

//...und hier z.B. der Wert 1
var regionsauswahl=document.getElementById('wdform_19_element8').value;

//..hier setze ich z.B. den (erhofften) "Variablennamen" rh_smwbs_1 zusammen und habe dann
// also einen Variablenwert erzeugt, welcher == dem Namen der Array-Variablen ist:

var erhoffter_arrayzugriff=saatgutauswahl+"_"+regionsauswahl;

//Problem: erhoffter_arrayzugriff.length ist immer "10" (rh_smwbs_1)
//...und eben nicht die Anzahl der Elemente aus dem Array "rh_smwbs_1"
var klickSaatgutAn = erhoffter_arrayzugriff.length;
alert (klickSaatgutAn);

});
Hat jemand eine Idee wie ich das Problem lösen kann?
Danke und Grüße, Kai
 

Sempervivum

Erfahrenes Mitglied
#2
Häufiges Problem: Wie greife ich auf eine Variable zu, deren Namen ich dynamisch erzeuge? Lösung: Die Variablen in einem Objekt ablegen, dann ist es ganz einfach:
Code:
var arr = {};
arr.rh_smwbs_1=["1","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
arr.rh_smwbs_2=["3","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
arr.rh_smwbs_3=["3","3","24","38","44","45","63","71","81","84","94","98","100","106","116","117","120","128","130","131","142","143","144","151","159","165","175","177","178","184","185","188","189","190","196","199","201","211","219"];
Dann kannst Du so zugreifen:
Code:
var erhoffter_arrayzugriff = arr[saatgutauswahl+"_"+regionsauswahl];
Es geht auch über das window-Objekt, aber das ist Trickprogrammierung und weniger zu empfehlen.
Auch mit eval, aber noch weniger zu empfehlen.
 
Status
Dieses Thema wurde gelöst! Zur Lösung gehen…