ankpro30 kumar
ankpro30 kumar

Reputation: 1

Issue parsing data through JQuery

I am getting the issue when parsing the following string data. Please let me know how can I parse it.

$(document).ready(function(){
     var abc = ['hello','Yes','No'];
     alert(abc);
     var abc1 = abc.split(",");
     $.each(abc1,function(i){
         alert(abc1[i]);
     });
});      

I want to parse the abc value. I need hello, yes and no value in variable so I can use it.

Upvotes: 0

Views: 52

Answers (6)

Vasim Shaikh
Vasim Shaikh

Reputation: 4542

$(document).ready(function(){
    var abc=['hello','Yes','No'];
    alert(abc);
    for (var i = 0; i < abc.length; i++) 
    {   
        alert(abc[i]);
    }
}

Upvotes: 0

Bar Goldinfeld
Bar Goldinfeld

Reputation: 221

If I understand you correctly, all you need is abc in one variable? You can use var jointAbc = abc.join(""); for that. If you want them separate than you already have the array.

Upvotes: 0

dexhering
dexhering

Reputation: 422

You can use the .forEach method from Array Object:

$(document).ready(function(){
    var abc=['hello','Yes','No'];
    alert(abc);
    abc.forEach(function(el){
        console.log(el);
    });
});

For more information: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/forEach

Upvotes: 0

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11717

Use forEach

The forEach() method executes a provided function once per array element.

ES5 way:

var abc=['hello','Yes','No'];
abc.forEach(function(e){
  alert(e);
})

ES6 way:

var abc=['hello','Yes','No'];
abc.forEach(e=>{
  alert(e);
})

You dont need jQuery to iterate an array.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68433

abc is already an array and an array doesn't have a split function.

just make it

$(document).ready(function(){
     var abc=['hello','Yes','No'];
     $.each(abc,function(i){
         alert(abc[i]);
     });
});

Upvotes: 1

tymeJV
tymeJV

Reputation: 104795

You have an array, not a string. You can loop the array:

var abc=['hello','Yes','No'];
for (var i = 0; i < abc.length; i++) {
    console.log(abc[i]);
}

Or make an actual string:

var abc = "hello,Yes,No";
var split = abc.split(",");

for (var i = 0; i < split.length; i++) {
    console.log(split[i]);
}

Upvotes: 0

Related Questions