🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 键名和键值分开作为一个新的json对象 I have a json object like this actually it is coming dynamically. For example we can use this Json object. ``` var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 }; ``` But i want to change it some thing like this using javascript or jquery. ``` var sample = [ { "label":"one", "value":1 }, { "label":"two", "value":2 }, { "label":"three", "value":3 }, { "label":"four", "value":4 }, { "label":"five", "value":5 } ]; ``` ## 方法 ``` var JsonData= { "one":1, "two":2, "three":3, "four":4, "five":5 }; var arr = []; for(property in JsonData) { arr.push( {"label":property, "value":JsonData[property]}) }; ``` ## 分析 1、定义一个新的对象 2、遍历,采用push赋值 3、采用 `JsonData[key]` 方法获取键名,`key`获取键值 4、输出