javascript遍歷json對象的key和任意js對象屬性實例
更新時間:2017年03月09日 10:14:46 投稿:jingxian
下面小編就為大家?guī)硪黄猨avascript遍歷json對象的key和任意js對象屬性實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
使用 keys 方法獲取該對象的屬性和方法:
function Pasta(grain, width, shape) { this.grain = grain; this.width = width; this.shape = shape; this.toString = function () { return (this.grain + ", " + this.width + ", " + this.shape); } } var spaghetti = new Pasta("wheat", 0.2, "circle"); var arr = Object.keys(spaghetti); document.write(arr);
結(jié)果圖:
顯示 Pasta 對象中以字母“g”開頭的所有可枚舉屬性的名稱:
function Pasta(grain, width, shape) { this.grain = grain; this.width = width; this.shape = shape; } function CheckKey(value) { var firstChar = value.substr(0, 1); if (firstChar.toLowerCase() == "g") { return true; } else { return false; } } var polenta = new Pasta("corn", 1, "mush"); var keys = Object.keys(polenta).filter(CheckKey); document.write(keys);
結(jié)果如圖:
遍歷json對象的鍵:
var an_obj = { 100: 'a', 2: 'b', 7: 'c', "name": "wu", "interesting": "Game" }; document.write(Object.keys(an_obj));
結(jié)果如圖:
以上這篇javascript遍歷json對象的key和任意js對象屬性實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
webpack中splitChunks分包策略的實現(xiàn)
splitChunks是 webpack 中用于分包的配置選項之一,本文主要介紹了webpack中splitChunks分包策略的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06ts依賴引入報錯:無法找到模塊“xxxxxx”的聲明文件問題解決
這篇文章主要給大家介紹了關(guān)于ts依賴引入報錯:無法找到模塊“xxxxxx”的聲明文件問題的解決辦法,文中通過示例帶將解決辦法介紹的非常詳細,需要的朋友可以參考下2023-07-07js數(shù)字滑動時鐘的簡單實現(xiàn)(示例講解)
下面小編就為大家?guī)硪黄猨s數(shù)字滑動時鐘的簡單實現(xiàn)(示例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08