我需要在逗号分隔列表中添加一个值
我的代码
var Plist, Llist;
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist = // Add comma separated value
} else if (id.startsWith("L")) {
Llist = // add comma repeated value
}
}
请建议更好的解决方案......
答案 0 :(得分:3)
var Plist = "", Llist = "";
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist += id + ",";
} else if (id.startsWith("L")) {
Llist += id + ",";
}
}
if (Plist.indexOf(',') !== -1) {
Plist = Plist.substring(0, Plist.length - 1);
}
if (Llist.indexOf(',') !== -1) {
Llist = Llist.substring(0, Llist.length - 1);
}