admin管理员组

文章数量:1023282

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?

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?

Share Improve this question asked May 19, 2012 at 15:15 AndrewAndrew 5288 silver badges22 bronze badges 3
  • Why is multiArray['CCC'] not an array? – kojiro Commented May 19, 2012 at 15:20
  • as JS can't have native associative array Interesting. Could you elaborate? – KooiInc Commented May 19, 2012 at 15:37
  • @KooiInc Javascript has indexed arrays and objects. That's probably what Andrew means. – inhan Commented May 19, 2012 at 15:42
Add a ment  | 

2 Answers 2

Reset to default 3

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

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}} };

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?

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?

Share Improve this question asked May 19, 2012 at 15:15 AndrewAndrew 5288 silver badges22 bronze badges 3
  • Why is multiArray['CCC'] not an array? – kojiro Commented May 19, 2012 at 15:20
  • as JS can't have native associative array Interesting. Could you elaborate? – KooiInc Commented May 19, 2012 at 15:37
  • @KooiInc Javascript has indexed arrays and objects. That's probably what Andrew means. – inhan Commented May 19, 2012 at 15:42
Add a ment  | 

2 Answers 2

Reset to default 3

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

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}} };

本文标签: How to loop through multidimensional associative javascript arrayStack Overflow