How to use forEach in vueJs? How to use forEach in vueJs? vue.js vue.js

How to use forEach in vueJs?


You can also use .map() as:

var list=[];response.data.message.map(function(value, key) {     list.push(value);   });


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


In VueJS you can use forEach like below.

let list=[];$.each(response.data.message, function(key, value) {     list.push(key);   });

So, now you can have all arrays into list . use for loop to get values or keys