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
2 Answers
Reset to default 3Ok, 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
2 Answers
Reset to default 3Ok, 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
版权声明:本文标题:How to loop through multidimensional associative javascript array? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745571637a2156772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论