1

I have an object lengthOfStay in which I'm trying to concatenate the values inside it and put it on 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(" ")
      }
}

so for the concatenation looks good and the result for example is

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

how can I concatenate a string on it ? so that the result would be

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

2 Answers 2

4

Try this:

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

FOR FIRST KEY UPPERCASE

Object.keys(this.lengthOfStay).map(k =>  {
   return `${this.lengthOfStay[k]} ${k.charAt(0).toUpperCase() + k.slice(1)}.` 
}).join(" & ")
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Object.entries() to get both key and value from object, then change first character to uppercase and concatenate in desired format

let data ={ lengthOfStay:{ yrs: '2', mos:'2' }}
let capFirstChar = (str)=> str[0].toUpperCase() + str.substr(1,)

let op = Object.entries(data.lengthOfStay)
               .map(([key, value]) => `${value} ${capFirstChar(key)}.`)
               .join(' & ')

console.log(op)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.