利用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":"通过"}}
这是第二个参数吧!第三个参数是控制格式化缩进空格的。