Printing object's keys and values

后端 未结 7 1039
暗喜
暗喜 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:52

    for loop for array and for..in iteration for object:

    var filters = [{ "user": "abc"}, {"application": "xyz"}];
    
    for (var i = 0; i < filters.length; i++) { // the plainest of array loops
      var obj = filters[i];
      // for..in object iteration will set the key for each pair
      // and the value is in obj[key]
      for (var key in obj) { 
        console.log(key, obj[key])
      }
    }

    ES6

    [{ "user": "abc"}, {"application": "xyz"}].forEach(
      obj => console.log(Object.entries(obj).flat())
    )

提交回复
热议问题