1

I have 'multidimensional associative' javascript array (which in fact is object with properties as JS can't have native associative array):

var multiArray={ AAA:"one", BBB:"two", CCC:{ 0:"xxx", 1:"yyy", 2:"zzz"} };

And i need to get such string from this array:

'AAA=one & BBB=two & CCC=xxx,yyy,zzz'

How do I do that?

If i use two simple loops like this:

for(var key in multiArray)
{
        for(var subkey in multiArray[key])
        {
            string = string + multiArray[key][subkey]+",";
        }
}

I get something like this:

'AAA = o,n,e & BBB = t,w,o & CCC = xxx, yyy,zzz'

Which is not what i need.

Any solutions using Javascript only?

3
  • Why is multiArray['CCC'] not an array? Commented May 19, 2012 at 15:20
  • as JS can't have native associative array Interesting. Could you elaborate? Commented May 19, 2012 at 15:37
  • @KooiInc Javascript has indexed arrays and objects. That's probably what Andrew means. Commented May 19, 2012 at 15:42

2 Answers 2

3

Ok, i've created a fiddle over here: http://jsfiddle.net/bJ6HH/. It works for any depth of nestedness.

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

4 Comments

Yes, this answer works. The key here is to check if property is object: 'if(typeof o[k] == 'object')' ;
then why did someone downvote this? I know inhan's answer is more involved and detailed, but for this simple scenario, i found my method simpler...
Yeah I agree, your code is actually better. I just added the array functionality in my suggestion. I wouldn't downvote your solution. Somebody must have mistakenly done that.
@Andrew typeof o[k] == 'object' in a way accomplishes that. But it it were an indexed array you would need to use o[k] instanceof Array instead since the Array class extends Object.
1

I would use a function like

var multiArray={ AAA:"one", BBB:"two", CCC:{ 0:"xxx", 1:"yyy", 2:"zzz"} };

function objToStr(o,delim) {
    if (/^(string|boolean|number)$/.test(typeof o)) return o;
    delim = delim || '&'; // delimiter
    var arr = [], isArray = true;
    for (var j in o) {
        if (isNaN(parseInt(j))) { isArray = false; break; }
    }
    if (isArray) {
        for (var j in o) arr[j] = objToStr(o[j],delim);
        return arr.join(',');
    }
    for (var j in o) {
        if (typeof o[j] != 'object') arr.push(j+'='+o[j]);
        else arr.push(j+'='+objToStr(o[j],delim));
    }
    return arr.join(delim);
}
console.log(objToStr(multiArray,'&'))

EDIT: You will need to escape necessary characters here if this is going to be a GET query. Also, I'm not sure what you're expecting as the result of the following array so I couldn't write the best code that will suit your needs.

var multiArray={ AAA:"one", BBB:"two", CCC:{ 0:"xxx", 1:"yyy", 2:{a:1, b:2}} };

2 Comments

Although Parth's answer has correct key point, his result string is not exactly correct. Your function returns what I exactly wanted. Thank you.
You're welcome. Though it's not final because of those 2 points I mentioned in the lower, edit section.

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.