我有一个字符串,我想在其中删除最后一个逗号并将其替换为句点。我想保留所有其他逗号。在这里,我尝试使用条件语句,它可以添加逗号,但无法用句点替换最后一个。我是新手,我非常感谢任何帮助。
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
displayResult();
}