Get name of key in key/value pair in JSON using jQuery?

前端 未结 3 1494
予麋鹿
予麋鹿 2021-01-13 23:17

Say I have this JSON:

[
    {
        \"ID\": \"1\",
        \"title\": \"Title 1\",
    },
    {
        \"ID\": \"2\",
        \"title\": \"Title 2\",
             


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 00:16

    Something like this, perhaps?

    items = [];
    for (key in jsonobj) {
        if (!itemExists(items, key)) {
            items[items.length] = key
        }
    }
    
    function itemExists(items, value) {
        for (i = 0; i < items.length; i++) {
            if (items[i] == value) {
                return true
            }
        }
        return false;
    }
    

    Of course, that will return items that exist in any one of the objects, not that exist in all. It's not entirely clear from your question if this is the solution you want.

提交回复
热议问题