Maison > Questions et réponses > le corps du texte
利用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":"通过"}}
三叔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);