Convert array to string in NodeJS

后端 未结 5 2001
面向向阳花
面向向阳花 2021-01-03 20:08

I want to convert an array to string in NodeJS.

var aa = new Array();
aa[\'a\'] = \'aaa\';
aa[\'b\'] = \'bbb\';

console.log(aa.toString());
<
5条回答
  •  梦毁少年i
    2021-01-03 20:58

    toString is a method, so you should add parenthesis () to make the function call.

    > a = [1,2,3]
    [ 1, 2, 3 ]
    > a.toString()
    '1,2,3'
    

    Besides, if you want to use strings as keys, then you should consider using a Object instead of Array, and use JSON.stringify to return a string.

    > var aa = {}
    > aa['a'] = 'aaa'
    > JSON.stringify(aa)
    '{"a":"aaa","b":"bbb"}'
    

提交回复
热议问题