We help IT Professionals succeed at work.

jQuery Detect When Last Value is Found?

894359
894359 asked
on
How do I detect when the last value is found in the below function? I don't do jQuery!  What I want is to add a pipe delimiter after each value is found but not the last value. So I'd have for example if it returns 3 values then I will get ==> value1|value2|value3 . The example below uses a console.log but it will actually be returning the value in a variable, in real life.

 $(function() {
	        $('[class*="solid-shape-large-rating-"]').each(function(i, e) {
		        var result = this.className.match(/solid-shape-large-rating-(.*?)\s+/);
		        console.log(result[1]);
			    
	        });
       });

Open in new window

Comment
Watch Question

leakim971Multitechnician
Distinguished Expert 2019

Commented:
$(function() {
    var results = [];
    $('[class*="solid-shape-large-rating-"]').each(function(i, e) {
        var result = this.className.match(/solid-shape-large-rating-(.*?)\s+/);
        console.log(result[1]);
        results.push(result[1]); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
    });
    console.log(results);
});

Open in new window

Author

Commented:
Hi Leakim,
No pipe delimiter. I want the values separated by a pipe as in my example, I will get ==> value1|value2|value3 .

Thanks!
Multitechnician
Distinguished Expert 2019
Commented:
here we go :
$(function() {
    var results = [];
    $('[class*="solid-shape-large-rating-"]').each(function(i, e) {
        var result = this.className.match(/solid-shape-large-rating-(.*?)\s+/);
        console.log(result[1]);
        results.push(result[1]); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
    });
    results = results.join("|");
    console.log(results);
});

Open in new window

Author

Commented:
Thank you sir!