我有一个对象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/

10-11 08:12