-1

how to iterate a JSON array using $.each(). My example data is dynamic which is changing base on the data that parse from the server. So my plan to format the data just like on the image but I don't get it. I use javascript which works properly but I need to use jQuery Ajax instead.

enter image description here

<script>
        var columns = {
            "data": [
                [
                    "ID",
                    "TYPE",
                    "TOTAL",
                    "1 bed room",
                    "2 bed room"
                ]
            ]
        };

        var testdata = {
            "data": [
                [
                    "100",
                    "Total Transaction Amount",
                    "9812355000",
                    "23397000",
                    "13976000"
                ],
                [
                    "100",
                    "No. of units",
                    "1268",
                    "3",
                    "2"
                ],
                [
                    "100",
                    "(Total sq.ft.)",
                    "",
                    "",
                    ""
                ],
                [
                    "100",
                    "Avg. price",
                    "7738450",
                    "7799000",
                    "6988000"
                ],
                [
                    "100",
                    "Avg. sq.ft.",
                    "",
                    "",
                    ""
                ],
                [
                    "100",
                    "Max. price",
                    "25494000",
                    "9918000",
                    "7318000"
                ],
                [
                    "100",
                    "Max. sq.ft",
                    "",
                    "",
                    ""
                ],
                [
                    "100",
                    "Min. price",
                    "5904000",
                    "6465000",
                    "6658000"
                ],
                [
                    "100",
                    "Min. sq.ft",
                    "",
                    "",
                    ""
                ]
            ]
        };

        var dynamicColumn = "", dynamicHeader = "";
        dynamicHeader += "<tr>";
        for (i = 0; i < columns.data[0].length; i++) {
            dynamicHeader += "<td>" + columns.data[0][i] + "</td>";
            console.log(columns.data[i]);
        }
        dynamicHeader += "</tr>";
        console.log(dynamicHeader);

        for (i = 0; i < testdata.data.length; i++) {
            var row = testdata.data[i];
            console.log(testdata.data[i]);
            dynamicColumn += "<tr>";

            for (ii = 0; ii < row.length; ii++) {
                dynamicColumn += "<td>" + (row[ii] === "" ? "0" : row[ii]) + "</td>";
                console.log(row);
            }

            dynamicColumn += "</tr>";
        }

        $('#thead').html(dynamicHeader);
        $('#tbody').html(dynamicColumn);

    </script>

While doing testing or example.

 $.each(data,
                        function (key, value) {
                            console.log(key);
                            console.log(value[0]);
                            console.log(value[1]);
                            console.log(value[2]);
                            console.log(value[3]);
                            console.log(value[4]);
                        });
3
  • Possible duplicate of JQuery $.each() JSON array object iteration Commented Mar 14, 2017 at 8:28
  • $.each(data,... you do not even have a variable named data Commented Mar 14, 2017 at 8:31
  • 1
    There is no such thing as a "json array". Commented Mar 14, 2017 at 8:40

1 Answer 1

0

See example below with your data

var testdata = {
  "data": [
    [
      "100",
      "Total Transaction Amount",
      "9812355000",
      "23397000",
      "13976000"
    ],
    [
      "100",
      "No. of units",
      "1268",
      "3",
      "2"
    ],
    [
      "100",
      "(Total sq.ft.)",
      "",
      "",
      ""
    ],
    [
      "100",
      "Avg. price",
      "7738450",
      "7799000",
      "6988000"
    ],
    [
      "100",
      "Avg. sq.ft.",
      "",
      "",
      ""
    ],
    [
      "100",
      "Max. price",
      "25494000",
      "9918000",
      "7318000"
    ],
    [
      "100",
      "Max. sq.ft",
      "",
      "",
      ""
    ],
    [
      "100",
      "Min. price",
      "5904000",
      "6465000",
      "6658000"
    ],
    [
      "100",
      "Min. sq.ft",
      "",
      "",
      ""
    ]
  ]
};

/**
 * Simple iterator
 */
$.each(testdata.data, function( index, value ) {
  alert( index + ": " + value );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

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.