gpt4 book ai didi

javascript - 如何将字符串组用单引号引起来

转载 作者:行者123 更新时间:2023-11-30 09:44:25 25 4
gpt4 key购买 nike

我需要将数组的元素导出为字符串,但在元素周围保留单引号。如您所见,将它们导出到 toString() 方法输出 Banana, Orange, Apple, Mango 但我需要像 'Banana', '橙色”、“苹果”、“芒果”

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var map = fruits.toString();

console.log(map);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我该怎么做?

最佳答案

ES6

var fruits = ["Banana", "Orange", "Apple", "Mango"];
// creates a new array containing: ["'Banana'", "'Orange'", "'Apple'", "'Mango'"]
var exportedFruits = fruits.map(fruit => "'" + fruit + "'");
console.log(exportedFruits.toString())

ES5

var fruits = ["Banana", "Orange", "Apple", "Mango"];
// creates a new array containing: ["'Banana'", "'Orange'", "'Apple'", "'Mango'"]
var exportedFruits = fruits.map(function(fruit){
return "'" + fruit + "'";
});
console.log(exportedFruits.toString())

关于javascript - 如何将字符串组用单引号引起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39521065/

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