ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## json将某一字段转化为字符串用逗号连接 ### 方法一:for,join方法 ``` var jsonArr = [ {"id": "1", "name": "A1"}, {"id": "2", "name": "A2"}, {"id": "3", "name": "A3"}, {"id": "4", "name": "A4"}, {"id": "5", "name": "A5"} ]; console.log(jsonArr); var a = [] ; for(var i=0;i<jsonArr.length;i++){ a.push(jsonArr[i]["id"]); } console.log(a); //[1,2,3,4,5] var b = a.join(','); console.log(b); //1,2,3,4,5 ``` ### 方法二:for,拼接,substring方法 ``` var jsonArr = [ {"id": "1", "name": "A1"}, {"id": "2", "name": "A2"}, {"id": "3", "name": "A3"}, {"id": "4", "name": "A4"}, {"id": "5", "name": "A5"} ]; console.log(jsonArr); var a = '' ; for(var i=0;i<jsonArr.length;i++){ a += jsonArr[i]['id'] + ','; } console.log(a); //1,2,3,4,5, //substr方法截止 console.log(a.substring(0,a.length-1)); //1,2,3,4,5 ```