If else statements do not work in filter

Can somebody please help me understand what I am doing wrong here. i have spend about 6 hours on this and no go what so ever.
All I want to do is filter through array InventoryTotal and when the id = to what i selected to set the inventory to that number else to set it to 0. No matter what I do the code gives me

22
0

So the value is set to 22 and than 0. For some reason the system does not stop after it found the if statement but than executes the else.

I also tried switch cases but they do not seem to work in vuejs.

            onChange(event) {
            this.fromLocation = event.target.value
            this.inventoryTotal.filter(function(elem){
                if(elem.location_id == event.target.value){
                  vm.tranferAmount = elem.stock; 
                  console.log(vm.tranferAmount);
                }else{
                    vm.tranferAmount = 0; 
                    console.log(vm.tranferAmount);
                }
                  
             });
           
        },

The Array.filter function loops through all items in the input array to construct a filtered array containing items for which the internal function returns true. That’s why the function doesn’t stop.

Instead you want Array.find:

onChange(event) {
    this.fromLocation = event.target.value
    var selectedItem = this.inventoryTotal.find(function(elem){
        return elem.location_id == event.target.value
    })
    vm.tranferAmount = selectedItem.stock; // what is vm.?
    console.log(vm.tranferAmount);
});

However, there is a better way of achieving what you want. Instead of putting the location_id and value on the DOM element, you can use the onChange event (or better still a v-model binding to bind fromLocation to the selected item’s location_id, then use computed properties to get the transferAmount.

Hopefully this jsfiddle demonstrates this for you: https://jsfiddle.net/Lu48t2dr/1/

wow thank you, i am really new to vue but this makes more sense. Thank you for putting all that together.