我想使用 reduce函数而不是这样做:
var result = '';
authors.forEach(
function(author) {
result += author.name + ', ';
}
);
console.log(result);
所以在数组 authors中有几个名称。现在我想用这个名字构建一个字符串,用逗号分隔(除了最后一个)。
var result = authors.reduce(function (author, index) {
return author + ' ';
}, '');
console.log(result);