How to Print Elements from Array with Javascript

How to Print Elements from Array with Javascript

I have array with elements for example array = ["example1", "example2", "example3"]. I don't know how to print in this format: 1. example1 2. example2 3. example 3...Any help?

0

7 Answers

Use forEach for this like below

var a = ["a", "b", "c"];
a.forEach(function(entry) {
  console.log(entry);
});

You can use a simple for loop:

for (i = 0; i < array.length; i++)
  document.writeln((i+1) + ": " + array[i]);

And use the document.writeln to print it out. See the below working snippet.

Snippet

array = ["example1", "example2", "example3"];
for (i = 0; i < array.length; i++)
  document.writeln((i+1) + ": " + array[i]);

Note: The document.writeln() is implemented differently many times. So you should use:

document.getElementById("id_of_div").innerHTML += (i+1) + ": " + array[i];
3

Try to use for loop :

for (var i=0; i<array.length; i++)
    console.log(i + ". " + array[i]);
0

You can use standard array methods to get the result you're after. MDN has some great documentation on array iteration methods.

var examples = ["example1", "example2", "example3"];

// You can use reduce to transform the array into result,
// appending the result of each element to the accumulated result.
var text = examples.reduce(function (result, item, index) {
    var item_number = index + 1;

    return result + " " + item_number + ". " + item;
}, "");

// You can use a variable with forEach to build up the
// result - similar to a for loop
var text = "";

examples.forEach(function (item, index) {
    var item_number = index + 1;

    text = text + " " + item_number + ". " + item;
});

// You can map each element to a new element which 
// contains the text you'd like, then join them
var text = examples.map(function (item, index) {
    var item_number = index + 1;
    return item_number + ". " + item;
}).join(" ");

// You can put them into an HTML element using document.getElementById
document.getElementById("example-text-result").innerHTML = text;

// or print them to the console (for node, or in your browser) 
// with console.log
console.log(text);

You can try this:-

var arr = ['example1', 'example2', 'example3']
for (var i = 0; i < arr.length; i++){
    console.log((i + 1) + '. ' + arr[i])
}

Use forEach for this like below(ES6)

var a = ["a", "b", "c"];
a.forEach((element) => {
        console.log(element)
    }
);

There are already plenty of good answers here. But instead of printing in a loop, I'd propose a simple functional transformation to generate a string that you can print (or use in another way):

var exemple = ["example1", "example2", "example3"]
var O = exemple.map( (e,i) => (i+1+"."+e) ).join(' ');
console.log(O);

It works by applying a .map() on your array, with an arrow function to combine each array element e and its index i into a string. You then just need to .join() the elements of the resulting array, using any separator you want (e.g. ' ').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.