How to use forEach in vueJs?
This is an example of forEach usage:
let arr = [];
this.myArray.forEach((value, index) => {
arr.push(value);
console.log(value);
console.log(index);
});
In this case, "myArray" is an array on my data.
You can also loop through an array using filter, but this one should be used if you want to get a new list with filtered elements of your array.
Something like this:
const newArray = this.myArray.filter((value, index) => {
console.log(value);
console.log(index);
if (value > 5) return true;
});
and the same can be written as:
const newArray = this.myArray.filter((value, index) => value > 5);
Both filter and forEach are javascript methods and will work just fine with VueJs. Also, it might be interesting taking a look at this:
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
You can also use .map() as:
var list=[];
response.data.message.map(function(value, key) {
list.push(value);
});