gpt4 book ai didi

javascript - 使用map.reduce 将数组转换为换行符分隔的字符串?

转载 作者:行者123 更新时间:2023-12-03 01:10:49 24 4
gpt4 key购买 nike

我认为应该可以使用map.reduce将数组转换为换行符分隔的字符串。但由于某种原因它不起作用。我做错了什么

copyLicenseCodesToClipboard = () => {
// tslint:disable-next-line:no-any
const licenseCodes = this.props.generateLicenseCodes.reduce((accumulator: any, element: LicenseCode) =>
accumulator.concat(element.code).concat('\n')
);
copyToClipboard(JSON.stringify(licenseCodes));
}

Uncaught TypeError: accumulator.concat is not a function

最佳答案

您还可以使用 mapjoin,在这种情况下这似乎更直观。

const licenseCodes = this.props.generateLicenseCodes.map((element)=>{return element.code;}).join("\n");

关于javascript - 使用map.reduce 将数组转换为换行符分隔的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52217746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com