0

var str = '01.jpg,02.jpg,03.jpg';
var arr = [str.split(',')];
var cnt = '';
arr.forEach(function(el){
		cnt += "<img class = 'imgall' src = 'imgt/" + el + "' alt='img'>";
	});
	console.log(cnt);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

What I need in console is:

<img class='imgall' src='imgt/01.jpg' alt='img'>
<img class='imgall' src='imgt/02.jpg' alt='img'>
<img class='imgall' src='imgt/03.jpg' alt='img'>  

Any help?

1
  • 1
    1. This has nothing to do with jQuery, 2. just don't wrap the return value of .split() (which is already an array) in an array. Commented Jul 13, 2019 at 9:14

5 Answers 5

2

You need not to wrap the result of splitting in another array (because the result of splitting is an array) and you could map the items and join them later to a single string.

var str = '01.jpg,02.jpg,03.jpg';
    arr = str.split(',');
    cnt = arr
        .map(function (el) {
            return '<img class="imgall" src="imgt/' + el + '" alt="img">';
        })
        .join('\n'); // take later '', without newline

console.log(cnt);

ES6 with a template literal

var str = '01.jpg,02.jpg,03.jpg';
    result = str
        .split(',')
        .map(s => `<img class="imgall" src="imgt/${s}" alt="img">`)
        .join('\n'); // take later '', without newline

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2
//correct usage;
var arr = str.split(',');

var str = '01.jpg,02.jpg,03.jpg';
var arr = str.split(',');
var cnt = '';
arr.forEach(function(el){
		cnt += "<img class = 'imgall' src = 'imgt/" + el + "' alt='img'>";
	});
	console.log(cnt);

Comments

1

var str = '01.jpg,02.jpg,03.jpg';
var arr = str.split(',');
var cnt = '';

for(var i=0;i<arr.length;i++)
{
		cnt += "<img class = 'imgall' src = 'imgt/" + arr[i] + "' alt='img'>";
}

console.log(cnt);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Comments

1

If you need a collection of DOM elements in a array, just try out this code:

var str = '01.jpg,02.jpg,03.jpg';
var arr = str.split(',');

var values = arr.map(function(image) {
  var img =  document.createElement('img')
  img.src = "/imgt" + image;
  img.alt = "img";
  return img;
})

console.log(values)

Comments

1

Do with your own existing code, just remove the [] from str.split(','); because str.split(',') already creates an array so no need to wrap it with another extra array notation here. Though I just added an extra \n nothing else.

var str = '01.jpg,02.jpg,03.jpg';
var arr = str.split(',');//see the change here
var cnt = '';

arr.forEach(function(el) {
  cnt += "<img class = 'imgall' src = 'imgt/" + el + "' alt='img'>\n";
});
console.log(cnt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.