0

Using the following JSON below, how do I loop the inner Errors and Messages values using jQuery:

JSON Format:

{
   "PagesCreated":0,
   "AssetsCreated":0,
   "AssetsUpdated":0,
   "Messages":[
      "Test message!",
      "Test message!",
      "Test message!"
   ],
   "Errors":[
      "Test error!",
      "Test error!",
      "Test error!"
   ],
   "EllapsedTime":"00:00:00.0000382"
}

Current jQuery in place on ajax success call:

$.each(data, function (key, value) {
    if (key == "PagesCreated") {
        console.log(value); // works
    }
    else if (key == "AssetsCreated") {
        console.log(value); // works
    }
    else if (key == "AssetsUpdated") {
        console.log(value); // works
    }
    else if (key == "EllapsedTime") {
        console.log(value); // works
    }
    else if (key == "Messages") {
        // TODO - How do I loop inner Messages values here?
    }
    else if (key == "Errors") {
        // TODO - How do I loop inner Errors values here?
    }
});

If it helps this is the c# object I am serializing to json:

public class MyObj
{
    public int PagesCreated { get; set; }
    public int AssetsCreated { get; set; }
    public int AssetsUpdated { get; set; }
    public TimeSpan EllapsedTime { get; set; }
    public List<string> Messages { get; set; }
    public List<string> Errors { get; set; }
}

Thanks for the help!

2 Answers 2

1

You can use .each method again:

$.each(data, function (key, value) {
    if (key == "PagesCreated") {
        console.log(value); // works
    }
    else if (key == "AssetsCreated") {
        console.log(value); // works
    }
    else if (key == "AssetsUpdated") {
        console.log(value); // works
    }
    else if (key == "EllapsedTime") {
        console.log(value); // works
    }
    else if (key == "Messages") {
        $.each(value, function (innerKey, innerValue) {
           console.log(innerValue);
       }
    }
    else if (key == "Errors") {
        $.each(value, function (innerKey, innerValue) {
           console.log(innerValue);
       }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked perfectly! I originally tried something similar, but was only doing: $.each(value) { }});
FYI, your example is missing closing tag just in case somebody runs into issue.
1

Please do not use jQuery to loop arrays. Plain JS works well and can be reused in all frameworks

You can test the type and react accordingly

const data = { "PagesCreated": 0, "AssetsCreated": 0, "AssetsUpdated": 0, "Messages": [ "Test message!", "Test message!", "Test message!" ], "Errors": [ "Test error!", "Test error!", "Test error!" ], "ElapsedTime": "00:00:00.0000382" };

Object.entries(data).forEach(([key, value]) => {
  if (typeof value == "object") {
    console.log(key, value.join(", "))
  } else console.log(key, value)
});

jQuery version - only difference is $.each(data,(key, value) => {

const data = { "PagesCreated": 0, "AssetsCreated": 0, "AssetsUpdated": 0, "Messages": [ "Test message!", "Test message!", "Test message!" ], "Errors": [ "Test error!", "Test error!", "Test error!" ], "ElapsedTime": "00:00:00.0000382" }

$.each(data,(key, value) => { 
  if (typeof value == "object") {
    console.log(key, value.join(", "))
  } else console.log(key, value)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

2 Comments

Thanks for the example using plain js. Alternatives are always good. However, the question was specific to jQuery so accepted the other answer as it was more relevant.
I added a jQuery version. I just had to change to $.each(data,(key, value) => {

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.