Get all keys of a JavaScript object

前端 未结 6 2187
遇见更好的自我
遇见更好的自我 2020-12-30 03:59

I was wondering if there was a quick way to extract keys of associative array into an array, or comma-separated list using JavaScript (jQuery is ok).

options         


        
6条回答
  •  青春惊慌失措
    2020-12-30 04:26

    You can use $.each() in jQuery:

    function keyArray(obj) {
      var rv = [];
      $.each(options, function(key) { rv.push(key); });
      return rv;
    }
    

    then

    var keys = keyArray(options);
    

    gives you ["key1", "key2"] as an array, which you could join to get a string.

提交回复
热议问题