将数组缩减为单个字符串
Reduce array to a single string
我想使用 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);
您正在重塑 join()
var authors = ["a","b","c"];
var str = authors.join(", ");
console.log(str);
如果你想使用 reduce 添加一个 if 检查
var authors = ["a","b","c"];
var result = authors.reduce(function (author, val, index) {
var comma = author.length ? ", " : "";
return author + comma + val;
}, '');
console.log(result);
因为我错过了映射部分来让人们开心...
var authors = [{
name: "a"
}, {
name: "b"
}, {
name: "c"
}];
var res = authors.map( function(val) { return val.name; }).join(", ");
console.log(res);
或
var authors = [{
name: "a"
}, {
name: "b"
}, {
name: "c"
}];
var result = authors.reduce(function(author, val, index) {
var comma = author.length ? ", " : "";
return author + comma + val.name;
}, '');
console.log(result);
试试这个:
var authors = ["Mikel", "Brad", "Jessy", "Pof", "MArting"]
var result = authors.reduce( (prev, curr) => prev +', '+ curr )
console.log(result)
对,所以它是一个对象。让我们先映射名称:
var result = authors.map(function( author ) {
return author.name;
}).join(', ');
刚刚收到了一堆答案,这里还有一个!
第一个选项是使用本机 js join 方法,它消除了 reduce 的需要。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
var authors = ['some author', 'another author', 'last author'];
var authorString = authors.join(",");
console.log(authorString);
重要 - 如果你的数组包含对象,那么你可能想在加入之前映射它:
var authors = [{name: 'some author'},{name: 'another author'},{name: 'last author'}]
var authorString = authors.map(function(author){
return author.name;
}).join(",");
console.log(authorString);
或者,如果您真的热衷于使用 reduce,只需确保在传入回调时使用之前的值、当前值和索引即可。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
var authorString = authors.reduce(function(prevVal,currVal,idx){
return idx == 0 ? currVal : prevVal + ', ' + currVal;
}, '')
console.log(authorString);
重要 - 同样,如果您的数组包含对象,那么您需要确保您使用的是 'name property':
var authors = [{name: 'some author'},{name: 'another author'},{name: 'last author'}];
var authorString = authors.reduce(function(prevVal,currVal,idx){
return idx == 0 ? currVal.name : prevVal + ', ' + currVal.name;
}, '')
console.log(authorString);
我也遇到过这个。
这些答案中的大多数都没有考虑到您想要 authors 名称,这意味着您有一个对象数组。
一行解决方案:
authors.reduce((prev, curr) => [...prev, curr.name], []).join(', ');
我想使用 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);
您正在重塑 join()
var authors = ["a","b","c"];
var str = authors.join(", ");
console.log(str);
如果你想使用 reduce 添加一个 if 检查
var authors = ["a","b","c"];
var result = authors.reduce(function (author, val, index) {
var comma = author.length ? ", " : "";
return author + comma + val;
}, '');
console.log(result);
因为我错过了映射部分来让人们开心...
var authors = [{
name: "a"
}, {
name: "b"
}, {
name: "c"
}];
var res = authors.map( function(val) { return val.name; }).join(", ");
console.log(res);
或
var authors = [{
name: "a"
}, {
name: "b"
}, {
name: "c"
}];
var result = authors.reduce(function(author, val, index) {
var comma = author.length ? ", " : "";
return author + comma + val.name;
}, '');
console.log(result);
试试这个:
var authors = ["Mikel", "Brad", "Jessy", "Pof", "MArting"]
var result = authors.reduce( (prev, curr) => prev +', '+ curr )
console.log(result)
对,所以它是一个对象。让我们先映射名称:
var result = authors.map(function( author ) {
return author.name;
}).join(', ');
刚刚收到了一堆答案,这里还有一个!
第一个选项是使用本机 js join 方法,它消除了 reduce 的需要。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
var authors = ['some author', 'another author', 'last author'];
var authorString = authors.join(",");
console.log(authorString);
重要 - 如果你的数组包含对象,那么你可能想在加入之前映射它:
var authors = [{name: 'some author'},{name: 'another author'},{name: 'last author'}]
var authorString = authors.map(function(author){
return author.name;
}).join(",");
console.log(authorString);
或者,如果您真的热衷于使用 reduce,只需确保在传入回调时使用之前的值、当前值和索引即可。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
var authorString = authors.reduce(function(prevVal,currVal,idx){
return idx == 0 ? currVal : prevVal + ', ' + currVal;
}, '')
console.log(authorString);
重要 - 同样,如果您的数组包含对象,那么您需要确保您使用的是 'name property':
var authors = [{name: 'some author'},{name: 'another author'},{name: 'last author'}];
var authorString = authors.reduce(function(prevVal,currVal,idx){
return idx == 0 ? currVal.name : prevVal + ', ' + currVal.name;
}, '')
console.log(authorString);
我也遇到过这个。 这些答案中的大多数都没有考虑到您想要 authors 名称,这意味着您有一个对象数组。
一行解决方案:
authors.reduce((prev, curr) => [...prev, curr.name], []).join(', ');