What's the fastest way to iterate over an object's properties in Javascript?

后端 未结 8 1703
旧巷少年郎
旧巷少年郎 2020-12-13 00:25

I know that I can iterate over an object\'s properties like this:

for (property in object)
{
    // do stuff
}

I also know that the fastest

8条回答
  •  长情又很酷
    2020-12-13 00:52

    You could alternatively use Object.getOwnPropertyNames to get the keys of the object.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

    var obj = {a:"a",b:"b"}
    ///{a: "a", b: "b"}
    var keys = Object.getOwnPropertyNames(a)
    ///(2) ["a", "b"]
    

提交回复
热议问题