Sum equivalent for string concatenation

Does there exist a d3 API that can perform the equivalent of d3.sum for string concatenation.

Like

const data = [{x:650,y:300,z:“1”,f:“a”},{x:650,y:150,z:“2”,f:“b”}];

const sum = d3.sum(data,d=>d.y);

const concatenated = data.reduce((acc, curr, index) => {
if (index === 0) {
return ${curr.f}${curr.z};
} else {
return ${acc},${curr.f}${curr.z};
}
}, ‘’);

console.log(sum, concatenated);

Thank you in advance

When sharing code, you can use the “preformatted text” option, which is a button that looks like </>, to make your code look much better.

There is no direct equivalent of d3.sum for strings, but you can do that reduce a bit more simply with map and join:

const concatenated = data.map(d => `${d.f}${d.z}`).join(",");
1 Like