Javascript reduce array to string

Reduce array to a single string

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join IMPORTANT — if you’re array contains objects, then you might want to map it before joining: or, if you’re really fired up about using reduce, just make sure you use the previous value, current value and index when passing in the callback. Example reduce() Syntax The syntax of the method is: Here, arr is an array. reduce() Parameters The method takes in: callback — The function to execute on each array element (except the first element if no initialValue is provided).

Reduce array to a single string

I would like to use the reduce function instead of doing this:

var result = ''; authors.forEach( function(author) < result += author.name + ', '; >); console.log(result); 

So in the array authors there are several names. Now I want to build a string with this names, separated by comma (except the last one).

var result = authors.reduce(function (author, index) < return author + ' '; >, ''); console.log(result); 

A flurry of answers just came in and here is one more!

The first option is using the native js join method which eliminates the need for 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); 

IMPORTANT — if you’re array contains objects, then you might want to map it before joining:

var authors = [,,] var authorString = authors.map(function(author)< return author.name; >).join(","); console.log(authorString); 

or, if you’re really fired up about using reduce, just make sure you use the previous value, current value and index when passing in the callback. 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); 

IMPORTANT — again if your array contains objects then you will want to make sure you are using the ‘name property’:

var authors = [,,]; var authorString = authors.reduce(function(prevVal,currVal,idx)< return idx == 0 ? currVal.name : prevVal + ', ' + currVal.name; >, '') console.log(authorString); 

Right, so it’s an object. Let’s map the names first then:

var result = authors.map(function( author ) < return author.name; >).join(', '); 
var authors = ["a","b","c"]; var str = authors.join(", "); console.log(str);

if you want to use reduce add an if check

var authors = ["a","b","c"];var result = authors.reduce(function (author, val, index) < var comma = author.length ? ", " : ""; return author + comma + val; >, ''); console.log(result);

Since I missed the mapping part to make people happy.

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);

I came across this also. Most of these answers dont take into account that you want the author s name, meaning you have an array of objects.

authors.reduce((prev, curr) => [. prev, curr.name], []).join(', '); 

JavaScript Array isArray() Method, The Array.isArray () was released with the release of ECMAScript5 JavaScript. This method simply checks whether the argument passed to its arguments is an array or not. This article will explain this Array isArray () method by explaining its syntax and then showcasing some examples. We will start by going over the …

Javascript Array reduce()

In this tutorial, we will learn about the JavaScript Array reduce() method with the help of examples.

The reduce() method executes a reducer function on each element of the array and returns a single output value.

Example
const message = ["JavaScript ", "is ", "fun."]; // function to join each string elements function joinStrings(accumulator, currentValue) < return accumulator + currentValue; >
// reduce join each element of the string let joinedString = message.reduce(joinStrings);
console.log(joinedString); // Output: JavaScript is fun.

reduce() Syntax

The syntax of the reduce() method is:

arr.reduce(callback(accumulator, currentValue), initialValue)

reduce() Parameters

The reduce() method takes in:

Оцените статью