Flatten object to array?

前端 未结 7 1220
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 03:21

I\'m using an object as a hash table. I\'d like to quickly print out its contents (for alert() for instance). Is there anything built in to convert a hash into

7条回答
  •  礼貌的吻别
    2021-01-05 03:57

    Since you want to alert it out I assume it's not for your production version, and that old browser compatibility is not an issue.

    If this is the case, then you can do this:

    var myHash = ......
    alert(Object.keys(myHash).map(function(key) { return [key, myHash[key]]; }));
    

提交回复
热议问题