Getting first JSON property

前端 未结 5 1970
闹比i
闹比i 2020-12-15 05:07

Is there a way to get the name of the first property of a JSON object?

I\'d like to do something like this:

var firstProp = jsonObj[0];
<
5条回答
  •  醉酒成梦
    2020-12-15 05:56

    When using jQuery, you can also use $.each to iterate over a JSON object. Looks cleaner IMO than using a for-loop

    var items = {
        'item1': 'content',
        'item2': 'content',
        'item3': 'content'
    }
    
    $.each(items, function() { 
        console.log($(this)[0])
    })
    

提交回复
热议问题