vue-typescript icon indicating copy to clipboard operation
vue-typescript copied to clipboard

Array idioms

Open davidmoshal opened this issue 8 years ago • 3 comments

Hi, am wondering what the current idioms are for Vue dat array handling in vue-typescript?

i.e:

  1. adding an element to an array?
  2. removing an element from an array?
  3. changing (updating) an element in an array?
  4. removing all elements from an arry?

Thanks David

davidmoshal avatar Aug 04 '16 04:08 davidmoshal

I'm not 100% sure what you're refering to,.

If you're talking about this, it works exactly the same way and in standard vuejs. In fact, the vue typings even add autocompletion for $set and $remove.

I personally use push() and splice() as well as standard array modifications (if it is an array of objects, else i use $set). To clear an array i guess this works: this.array = []

For this, i guess whatever you would do in Javascript Vue is valid in vue-typescript

Hopefully i didn't completely miss your point.

itsfrank avatar Aug 04 '16 14:08 itsfrank

Seems like these work on both standard Vue arrays, and Vuex arrays changed by mutations:

    // 1) remove all: 

    arr = [] 


    // 2) remove element by ID: apparently filter is efficient:

    arr =  arr.filter(o => o[id_name] != id)


    // 3) upsert element, and have its properties update the GUI too:

    const existing = _.find(arr, id_name, id)
        if( existing )
           _.assign(existing, child)
       else
          arr.push(child)

davidmoshal avatar Aug 04 '16 23:08 davidmoshal

Adding this purely as a comment for other searchers...

The following syntax works for Vue 2 and Typescript.

 constructor( private people: Administrator[]) {
           ... snip ....
 Replace( index: number, person: Administrator ) {
    Vue.set( this.people, index, person);
}

waratah avatar Oct 02 '17 23:10 waratah