1

I have a string like this:

string = "locations[0][street]=street&locations[0][street_no]=
         34&locations[1][street]=AnotherStreet&locations[1][street_no]=43";

What must I do with this string so i can play with locations[][] as I wish?

3
  • Where do you get the string from? It may be easier to create a proper JS object from the beginning. Commented Aug 1, 2011 at 13:05
  • 1
    Looks like you'll have to use split using & as delimeter then use eval to evaluate each part. Can't you have more "proper" data source though? Commented Aug 1, 2011 at 13:06
  • This is how i get my data and i can't do anything to change that unfortunately. Need to parse this as it is and right now i just can't thing at a way. Commented Aug 1, 2011 at 13:15

2 Answers 2

1

You could write a parser:

var myStr = "locations[0][street]=street&locations[0][street_no]=34&locations[1][street]=AnotherStreet&locations[1][street_no]=43";


function parseArray(str) {
    var arr = new Array();
    var tmp = myStr.split('&');
    var lastIdx;
    for (var i = 0; i < tmp.length; i++) {
        var parts = tmp[i].split('=');
        var m = parts[0].match(/\[[\w]+\]/g);
        var idx = m[0].substring(1, m[0].length - 1);
        var key = m[1].substring(1, m[1].length - 1);
        if (lastIdx != idx) {
            lastIdx = idx;
            arr.push({});
        }
        arr[idx * 1][key] = parts[1];
    }
    return arr;
}

var myArr = parseArray(myStr);
6
  • +1 nice solution, although I think you meant push and not add Commented Aug 1, 2011 at 13:43
  • @Joey - push is the instance method, which also would work. Array.add(instance, value) is adds a new index to an array. Commented Aug 1, 2011 at 13:45
  • it throws this: Uncaught TypeError: Object function Array() { [native code] } has no method 'add', tried push, same error Commented Aug 1, 2011 at 13:52
  • 1
    I'm just not sure you're getting much browser coverage with that, but either way. Commented Aug 1, 2011 at 13:52
  • @Joey - touche! Gomoi got precisely what you were talking about... will update w/ push, not .add.. Commented Aug 1, 2011 at 14:37
0

As Shadow wizard said, using split and eval seems to be the solution. You need to initialize locations first, if you want to avoid an error.

stringArray=string.split("&");
for (var i=0;i<stringArray.length;i++){
    eval(stringArray[i]);
}

However, you might need to pay attention to what street and street_no are. As is, it will produce an error because street is not defined.

Edit: and you'll need to fully initialize locations with as many item as you'll have to avoid an error.

2
  • 2
    Be very careful using eval like this with a string coming from an external source. Unless the source is 100% trusted this is very dangerous. Commented Aug 1, 2011 at 13:34
  • Yeah, i know this is some dangerous stuff, I try to avoid using it unless it's the only way (or if it's easier than the rest by far). Using regexp like Brian did seems a safer (and cleaner) way but a longer one. Commented Aug 1, 2011 at 13:43

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.