1. 程式人生 > >JSON.stringify提供了引數分離出自己需要的那部分資料

JSON.stringify提供了引數分離出自己需要的那部分資料

var data =[
{
    name: "程咬金",sex:"1",age:26    
},
{
    name: "程才",sex:"0",age:20
},
{
    name: "程新鬆",sex:"1",age:22    
},
{
    name: "程功",sex:"1",age:18    
}

];


var str_json = JSON.stringify(data,["name","sex"]);

console.log(str_json);

[{"name":"程咬金","sex":"1"},{"name":"程才","sex":"0"},{"name":"程新鬆","sex":"1"},{"name":"程功","sex":"1"}]

=====================================================================

var str_json = JSON.stringify(data,function(key,value){
    if(key == 'sex'){
        return ["女",'男'][value];
    }
    return value;
});
console.log(str_json);
VM73:7 [{"name":"程咬金","sex":"男","age":26},{"name":"程才","sex":"女","age":20},{"name":"程新鬆","sex":"男","age":22},{"name":"程功","sex":"男","age":18}]