Printing object's keys and values

后端 未结 7 1038
暗喜
暗喜 2021-02-01 06:04

I want to print a key: value pair from javascript object. I can have different keys in my array so cannot hardcode it to object[0].key1

var filters = [{\"user\"         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 06:49

    for (key in filters[0]){
        console.log( key + ": " + filters[0][key]);
    }
    

    Or if you want to print all the values of filters

    for (i in filters){
        console.log(i);
        for (key in filters[i]){
            console.log( key + ": " + filters[i][key]);
        }
    }
    

    On @mplungjan 's comment

    filters.forEach(function(obj, index){
        console.log(index);
        for (var key in obj){
            console.log(key, obj[key]);
        }
    });
    

提交回复
热议问题