JS遍历类、json对象属性、值,
我们在页面,经常会接收后面或json返回的数据,要一个一个赋值,遍历json属性,可以方便给界面的控件赋值。

代码:
遍历js类

    <script type="text/javascript">
        //定义一个普通的js类,包含方法
        var p= function (){
        this.Id= 1;
        this.Name= 'test1';
        }
        var pp= new p(); 
        for(var item in pp){
        if(typeof(pp[item])== "function")
        continue;
        console.log("p对象中"+item+"的属性="+pp[item]);
        }
    </script>

遍历Json对象

//遍历Json对象属性、值
    <script type="text/javascript">
        //定义一个jsonArray对象
        var json = [{ a: '121', b: 222, bb: 'vvv' }, { a: '122', b: 223, bb: 'ccc' }];
        for (var i=0;i<json.length;i++) {
            console.log(json[i]);
            var jsonItem = json[i];
            for (var item in jsonItem) {
                //遍历pp对象中的属性,只显示出 非函数的 属性,注意不能 遍历 p这个类
                if (typeof (jsonItem[item]) == "function")
                    continue;
                console.log("p对象中" + item + "的属性=" + jsonItem[item]);
            }
        }
    </script>
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2134cb94 * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance * fix ci_static_analysis_clang (ci_clang_tidy) * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance 8 天前
6057b31d * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * Use ubuntu-latest image to run Valgrind (#4575) * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Ubuntu image to run valgrind * Use Clang image to run iwyu (#4574) * :wrench: use Clang image to run iwyu * :wrench: use Clang image to run iwyu * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :art: format code * :hammer: clean up 10 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐