我使用的反应组件就像这样工作
<FontAwesomeIcon icon={faCoffee} />
它需要一个字体真棒图标,让它说地址簿之前添加fa,删除连字符和大写每个世界的第一个字母。
address-book become faAddressBook
copyright become faCopyright
arrow-alt-circle-down become faArrowAltCircleDown
是否可以创建一个javascript函数,它采用像这样的数组
["address-book","copyright","arrow-alt-circle-down"]
并将其转换为类似
的数组 ["faAddressBook","faCopyright","faArrowAltCircleDown"]
答案 0 :(得分:1)
您可以使用数组的map()
和forEach()
执行以下操作:
var arr = ["address-book","copyright","arrow-alt-circle-down"];
function upperCase(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
var outPut = arr.map(function(item){
var temp = item.split('-');
var temp2 = [];
temp.forEach(function(data){
temp2.push(upperCase(data))
});
return 'fa' + temp2.join('')
})
console.log(outPut);
答案 1 :(得分:1)
有一些方法可以做到这一点。喜欢使用正则表达式。但是,您的要求很简单,因此可以使用JavaScript拆分方法轻松完成。请检查以下实现的功能。
function myFunction() {
// get all of the answers
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var data = new Array();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
var record = {};
record.question = itemResponse.getItem().getTitle();
record.answer = itemResponse.getResponse();
data.push(record);
}
}
// check if there was response
if (data.length == 0)
return
// calculate cost based on the provided factors
var numberOfSibling = Number(data[1].answer)
var team = data[2].answer
var teamCost = 1;
if (team == 'team1')
teamCost = 1;
if (team == 'team2')
teamCost = 2;
if (team == 'team3')
teamCost = 3;
var fourEyesExists = data[3].answer
var fourEyesCost
if (fourEyesExists == "Yes" )
fourEyesCost = 4;
else
fourEyesCost = 1;
var cost = 25*numberOfSibling+(teamCost*2)+(fourEyesCost*12);
// Show the price to the kid
form.setConfirmationMessage('You are going to ask you dad to pay: $' + cost + '. May god be with you son. If your dad refused, offer to clean the dishes for: ' + cost/2 + ' days.');
// delete the response
form.deleteAllResponses();
}
答案 2 :(得分:0)
看起来这里有两个重要步骤。首先,我们需要在每个字符串前加fa
,其次,我们需要将它们从kebab-case转换为camelCase。
所以只需为每个转换编写一个函数,然后使用每个转换通过地图运行数组:
const kebabs = ["address-book","copyright","arrow-alt-circle-down"];
function kebabToCamel(str) {
return str.replace(/-(\w)/g, m => m[1].toUpperCase());
}
function prependFa(str) {
return "fa-" + str;
}
console.log(kebabs.map(prependFa).map(kebabToCamel))
&#13;