cari

Rumah  >  Soal Jawab  >  teks badan

JSON.stringify()方法第三个参数问题!

利用stringify()方法,return underfined;可以过滤key为name的键,但是一下过滤了所有key为name的键,怎么控制只过滤company对象下面的name呢?

var person = {
            name: "张三",
            age: 14,
            sex: "男",
            tell: 5033453,
            birthday:new Date(),
            company:{
                name:"百度",
                contry:"中国",
                boss:"马化腾",
                state:1
            }
        };

        var jperson = JSON.stringify(person,function(key,value){
            switch (key) {
                case "name":
                    return undefined;
                case "state":
                    return value===1?"通过":"未通过";
                case "birthday":
                    return value.replace("T"," ").replace("Z","").substr(0,19);
                default:
                    return value;
            }

        });
        console.log(jperson);

结果:{"age":14,"sex":"男","tell":5033453,"birthday":"2016-05-10 10:48:54","company":{"contry":"中国","boss":"马化腾","state":"通过"}}

高洛峰高洛峰2940 hari yang lalu705

membalas semua(2)saya akan balas

  • 欧阳克

    欧阳克2016-11-10 15:59:08

    这是第二个参数吧!第三个参数是控制格式化缩进空格的。

    balas
    0
  • 三叔

    三叔2016-11-10 15:58:29

    var person = {
        name: "张三",
        age: 14,
        sex: "男",
        tell: 5033453,
        birthday:new Date(),
        company:{
            name:"百度",
            contry:"中国",
            boss:"马化腾",
            state:1
        }
    };
    
    var jperson = JSON.stringify(person,function(key,value){
        switch (key) {
            case "company":
                delete value["name"];
                return value;
            case "state":
                return value===1?"通过":"未通过";
            case "birthday":
                return value.replace("T"," ").replace("Z","").substr(0,19);
            default:
                return value;
        }
    
    });
    console.log(jperson);


    balas
    0
  • Batalbalas