blu10
blu10

Reputation: 654

Using an array data item length as part of string in a vue computed property

I have a data item

items: [],

which i am using in a computed property that returns the lengh of that array as part of a string.

itemSummary : function() {
        return this.items.length === 0 ? "No Items" : "`${this.items.length}` items selected"
}

Is it possible to do this with string interpolation....?

Upvotes: 0

Views: 251

Answers (1)

Marcus
Marcus

Reputation: 2321

You almost had it in your question. I think the code should be:

itemSummary() {
    return this.items.length === 0 ? "No Items" : `${this.items.length} items selected`
}

With an interpolated string you put everything inside back quotes and interpolate variables or code snippets by wrapping them in ${ ... }.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Upvotes: 1

Related Questions