簡體   English   中英

如何在對象數組上使用 jQuery.each() 循環

[英]How can I use jQuery.each() loop on Array of Object

我有這個對象數組,如何使用 jQuery.each() 遍歷它?

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [parent_cat_id] => 1
            [child_cat_name] => Java
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

    [1] => stdClass Object
        (
            [id] => 2
            [parent_cat_id] => 1
            [child_cat_name] => JavaScript
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

    [2] => stdClass Object
        (
            [id] => 3
            [parent_cat_id] => 1
            [child_cat_name] => HTML
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

    [3] => stdClass Object
        (
            [id] => 4
            [parent_cat_id] => 1
            [child_cat_name] => PHP
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

    [4] => stdClass Object
        (
            [id] => 5
            [parent_cat_id] => 1
            [child_cat_name] => Python
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

    [5] => stdClass Object
        (
            [id] => 6
            [parent_cat_id] => 1
            [child_cat_name] => Ruby
            [status] => 1
            [date] => 2016-09-11 01:26:00
        )

)

我正在嘗試使用這個 -

$.each( data, function( key, value ) {
    console.log( value );
});

這給了我以下錯誤 -

類型錯誤:無效的“in”操作數 e

你的數組有奇怪的格式。 看這個例子:

        var data = [    
            {text: "hello"},
            {text: "good bye"},
            {text: "Hello again"}       
        ]

        $.each( data, function( key, value ) {
            console.log( value.text );
        });

我建議你改用 forEach。 jQuery.each 方法有另一個目標。

data.forEach(function(entry) {
    //Your logic.
});

您的 jQuery.each 語法是正確的,但正如 Karl-André Gagnon 所提到的,這是 PHP 數組的輸出。

在將數組發送到前端之前通過json_encode傳遞數組。

http://php.net/manual/en/function.json-encode.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM