Jquery for each loop of arrays inside an array

I have a function that returns an array that will contain an unknown number of associative arrays and I need help iterating through those arrays to get the key and values.

Here is an example array:

[{"name":"test","value":"1234"},{"name":"test2","value":"5678"}] 

Open in new window


I need to use Jquery to iterate through each array of those arrays and alert the values (I will use them later but for now alerting is fine).
I took a crack at it and this is what I have so far in my first attempt:

For my tests the variable data is set to that array above.

EDIT: I just found that the array in the data variable is actually a string. So I suppose I will need to convert to an array somehow. I have been trying to do this, I will post if I come up with anything.

$.each(data, function( key, value ) {
                $.each(key, function( k, v ) {
                    alert( "Key: " + k + ", Value: " + v );
                });
            });

Open in new window


I get this error using the code above:
Uncaught TypeError: Cannot use 'in' operator to search for '68' in [{"name":"test","value":"1234"},{"name":"test2","value":"5678"}]
ITNCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
something like this?
$(document).ready(function(){
    var data = [{name:"test",value:"1234"},{name: "test2",value:"5678"},{name:"With subarray",value: ["one","two","three"]}];
    jQuery.each(data, function( k, v ) {
        if (jQuery.isArray(v.value)) {
                $.each(v.value, function( i, v2 ) {
                        $("#outp").append( "<li>Index: " + i + ", Value: " + v2 + "</li>"); });
        } else {
            $("#outp").append( "<li>Index: " + k + ", Value: " + v.value + "</li>");
        }

            });

});

Open in new window

Sample:
http://jsfiddle.net/EE_RainerJ/wa7j4r98/

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITNCAuthor Commented:
I actually managed to get it working but since you provided a working example I will go ahead and accept yours as the answer. I forgot that I could set the data type on my ajax POST to JSON and have it process it itself. Then I used the following code to get the data (I went ahead and added the rest of the code I was planning on using in place of alerts):

$.each(data, function( key, value ) {
                var obj = jQuery.parseJSON(value);
                $('#tags' + uuid + '').tagsinput('add', { "uuid": obj.value , "tagName": obj.name});
            });

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.