Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get each value from a comma separated string in JavaScript?

Tags:

javascript

Lets consider I have a string called

string s = "jpeg, jpg, gif, png";

So from this I need to get each one like, I can assign each extension to one var variable such as

var a = jpeg
var b = jpg
var c = gif
var d = png

Same way if I will add more extensions to the string then accordingly I will have to get all with same var variable.

like image 788
Tripati Subudhi Avatar asked Dec 10 '22 02:12

Tripati Subudhi


1 Answers

All you have to do is to use the javascript split method-

$(function(){
    var s = "jpeg, jpg, gif, png";
    var match = s.split(', ')
    console.log(match)
    for (var a in match)
    {
        var variable = match[a]
        console.log(variable)
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
like image 190
Grooveek Avatar answered Dec 11 '22 15:12

Grooveek