简体   繁体   English

如何添加 append 并加入数组中的每个项目?

[英]How can I prepend, append and join each item in an array?

myArr = [1,2,3,4,5]

I would like to join but also prepend and append to each item我想加入,但也想在每个项目前面加上 append

Result: '$'1'^'|'$'2'^'|'$'3'^'|'$'4'^'|'$'5'^'|'$'6'^'结果: '$'1'^'|'$'2'^'|'$'3'^'|'$'4'^'|'$'5'^'|'$'6'^'

I have tried this:我试过这个:

console.log("$" + valArr.join("'|'") + "^");

which results in: '$'1'|'2'^'结果是: '$'1'|'2'^'

I have tried: valArr.map(i => "'$'" + i).join("|");我试过: valArr.map(i => "'$'" + i).join("|"); but this only appends.但这只是附加。

First map the array and then join it,首先 map 阵列然后加入它,

 const myArr = [1, 2, 3, 4, 5].map((i) => `'$'${i}'^'`).join("|"); console.log(myArr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM