gpt4 book ai didi

javascript - 在 map 函数javascript上连接字符串

转载 作者:行者123 更新时间:2023-11-28 14:18:24 26 4
gpt4 key购买 nike

我有一个对象 lengthOfStay,我试图在其中连接其中的值并将其放在 customerApplication.lengthOfStay

lengthOfStay:{
yrs: '',
mos:''
},
customerApplication: {
length_of_stay: this.lengthOfStayYrsMos
}

computed: {
lengthOfStayYrsMos(){
return this.customerApplication.length_of_stay =
Object.keys(this.lengthOfStay).map(k => this.lengthOfStay[k]).join(" ")
}
}

因此串联看起来不错,例如结果是

lengthOfStay:{
yrs: '2',
mos:'2'
},
//result
customerApplication: {
length_of_stay: "2 2"
}

如何在其上连接字符串?这样结果就是

customerApplication: {
length_of_stay: "2 Yrs. & 2 Mos."
}

最佳答案

试试这个:

Object.keys(this.lengthOfStay).map(k =>  {
return `${this.lengthOfStay[k]} ${k}.`
}).join(" & ")

第一个键大写

Object.keys(this.lengthOfStay).map(k =>  {
return `${this.lengthOfStay[k]} ${k.charAt(0).toUpperCase() + k.slice(1)}.`
}).join(" & ")

关于javascript - 在 map 函数javascript上连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56388724/

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