[Vuejs]-JS Pricebreak Calculator. Find Array value from qty

0👍

Since you’re using vue.js, you could add the method under watchers. So you can have something like this

watch: {
  qty (val) {
    if (val > 11) {
      let res = 0;
      // The block gets the nearest value in the breaks array
      let arr = this.breaks;
      let diff = Math.abs(arr[0] - val);
      for (let i = 0; i < arr.length-1; i++) {
        if (val == arr[i]) {
          res = i;
        }

        if (Math.abs(arr[i] - val) < diff) {
          res = i
        }
      }
      // Price per shirt will be the value at index of i
      let PPS = this.matrix[i];
    }
  }
}

I hope this helps

Leave a comment