[Vuejs]-Print array in method in vue.js

1👍

You can use the following method:

let array = [1, 2, 3, 4];
function printArrayElements(array){
    array.forEach(element => console.log(element))
}

printArrayElements(array);

You can achieve that using the while loop as well:

let array = [1, 2, 3, 4]
function printArrayElement(array){
    let index = 0;
    while (index < array.length){
        console.log(array[index]);
        index +=1;
    }
}

printArrayElement(array);

1👍

It’s more about javascript than it is vue. To the point:

You have a function called toString that does that for you.
Here’s a snippet:

const stuff = [0, 1, "Apple", "Mango"];
const x = fruits.toString();
console.log(x);

1👍

You’ve the possibility to use the join method and show the items separated by space :

printStringArray(objectWithArray) {
      let joined=objectWithArray.join(" ");
      console.log(joined)
     return joined;
    },

Leave a comment