Changes value of an array in VUE by filtering by another parameter

-1

I have the following array in VUE:

acoes : [
            { name: 'Quero Vender', id:'quero-vender', active: false },
            { name: 'Quero Comprar', id:'quero-comprar', active: true },

            ] , 

I'm passing the array id field to a function I need to set the active field to true from the record where the ID is 'want-to-buy', how can I do it?     

asked by anonymous 14.12.2018 / 03:33

1 answer

1

I do not know anything about Vue but your question is more about JS than Vue.

You can use a ARRAY MAP . The Array map will return a new array with the modifications you want, in this case for each item in the array I modify the active attribute by checking if the value is 'want-buy' and return the item itself.

methods: {
  funcaoManeira: function ($arr) {
      reuturn $arr.map(function($a){ 
           $a.active = $a.id == 'quero-comprar';
           return $a;
      })
  }
}
    
14.12.2018 / 11:29