> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/tools/object-method.md).

# Object Method

v

<table><thead><tr><th width="233">Method</th><th width="190"></th><th>example</th></tr></thead><tbody><tr><td>Object.hasOwn(obj, key) </td><td>取代舊的 Object.hasOwnProperty</td><td><p>let b = ["a", "b",   ,"d"]<br>for (let i = 0; i&#x3C; b.length; i++){</p><p>   if (Object.hasOwn(b, i)) {   </p><p>      console.log(b[i])</p><p>   }</p><p> }; // 'a'. 'b' 'd'</p></td></tr><tr><td>Object.prototype.toString.call()</td><td>判斷是哪種型別好用</td><td><pre><code>Object.prototype.toString.call([1,2]) // "[object Array]"
Object.prototype.toString.call(1); // '[object Number]'
Object.prototype.toString.call('string') // '[object String]'
Object.prototype.toString.call({a: [1,2]}) // '[object Object]'
</code></pre></td></tr><tr><td>Object.keys(obj)</td><td>Array 也是 Object 一種所以也可以</td><td>Object.keys({a: 1, b: 2}) // [a, b]<br>Object.keys(["a", "b"]) // [0, 1]</td></tr><tr><td>Object.values(obj)</td><td></td><td>Object.values({a: 1, b: 2}) // [1, 2] Object.values(["a", "b"]) // [1, 2]</td></tr><tr><td>Object.entries(obj)</td><td></td><td>Object.entries({a: 1, b: 2}) // [[a, 1], [b,2]] Object.entries(["a", "b"]) // [[0,a], [1,b]]</td></tr><tr><td>Object.fromEntries(arr)</td><td>跟 Object.entries(obj) 相反</td><td>Object.fromEntries([[0,'a'], [1,'b']]) // [{0: 'a', 1: 'b'}</td></tr></tbody></table>
