Change the value of a VUE array by filtering by another parameter

Asked

Viewed 27 times

-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 am passing the array id field to a function I need to set the active field to true of the record where the ID is 'want-to-buy', as I can do?

  • Your question is good but you need more details. Could I provide? can I help you...

  • Without enough information you can’t know what you need. That other question of yours already solves your problem, but I believe it is a XY problem. What you want to do and what you’ve tried?

1 answer

1


I don’t know anything about Vue but his doubt 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 of 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;
      })
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.