0

I'm using a one-off language similar to javascript in syntax, so an answer in that more common language should suffice.

I have a list of name/val pairs that i built from a big GET string that looks something like

"n1=v1,n2=v2..."

I'm not sure that my initial approach is correct. I used a primitive in this language

tolist(GETstring,"=")

to split the name value pairs into the above list. Perhaps, this is the wrong approach from the gate.

This gives me

data = [["n1","v1"],["n2","v2"],...]

I'm trying to change this into a named array, such as

data["n1"]="v1";
data["n2"]="v2";
...

so that I can access items by name, not by list index (as it is highly volitale)

What is the better approach to getting the data in this format. I've tried a few including evals but nothing seems to work.

BReal14
  • 1,603
  • 1
  • 12
  • 35
  • If ordering is not important (or the data can be sorted later) and the keys all map nicely to strings, use a "normal object" as a Map/Dictionary. Using a loop over the existing data in this case is fairly trivial and there is no need for `eval`. – user2864740 Oct 15 '13 at 18:11

2 Answers2

2

You'll have to split the string up then iterate through it.

var obj = {};
var originalString = "n1=v1,n2=v2";
var splitOriginalString = originalString.split(",");
for (var i = 0; i < splitOriginalString.length; i++) {
    var tmpObj = splitOriginalString[i].split("=");
    obj[tmpObj[0]] = tmpObj[1];
}
Dropzilla
  • 502
  • 3
  • 14
  • I think you want to use `for (var k = 0; k < splitOriginalString.length; k++)` since you're accessing an array rather than object properties. – Todd Oct 15 '13 at 18:31
  • The above code works as expected [http://jsfiddle.net/byYaH/1/](http://jsfiddle.net/byYaH/1/) – Dropzilla Oct 15 '13 at 18:48
  • While it works, it's not a great idea. For example, see here: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea (and since it's so easy just to use the other method, why not use it?) – Todd Oct 15 '13 at 18:52
  • @Todd, I have updated the answer since the post your referred suggest that when working with arrays in javascript the processor will sometimes get the length wrong. I have yet to experience that result, but as you said, it's easy to use the alternative method so why not use it. Plus using `for(var i =0;...` will perform better if iterating over a large set of data. – Dropzilla Oct 15 '13 at 19:02
  • Thanks, i've altered this a bit to fit the language and i have some object pairs now :) – BReal14 Oct 15 '13 at 19:52
0

There is no option to do it. You've got two ways to do workaround.

  1. Create two arrays, one for keys and one for values.

    var indexes = ["test", "test2"];
    var values = ["val", "val2"];
    var value = values[indexes.indexOf("test2")]; // will get "val2"
  2. Create nested array with key 0 for your string key and with 1 for its value.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Elon Than
  • 9,603
  • 4
  • 27
  • 37