javascript - อาร์กิวเมนต์ replacer ของเมธอด JSON.Stringify ไม่ทำงานกับวัตถุที่ซ้อนกัน
ฉันมีวัตถุหนึ่ง ฉันต้องการส่งเวอร์ชันที่เรียบง่ายของวัตถุนั้นไปยังเซิร์ฟเวอร์
{
"fullName": "Don Corleone",
"actor": {
"actorId": 2,
"name": "Marlon",
"surname": "Brando",
"description": "Marlon Brando is widely considered the greatest movie actor of all time... ",
"heroList": [],
"photo": "C:\\projects\\files\\actor\\1532955376934.png"
},
"heroProfilePhoto": "data:image/png;base64,/9j/...
"production": {
"title": "The Godfather",
"imdbRate": 9.2,
"genre": "8",
"releaseDate": "1972-03-23T21:00:00.000Z",
"director": "Francis Ford Coppola",
"writer": "Mari Puzo",
"detail": "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son."
}
}"
ฉันมีคำถามสองข้อ::
1) เป็นไปได้ไหมที่จะดึงบางสิ่งแบบนี้ด้วยพารามิเตอร์ replacer ของ JSON.stringify()?
{
"fullName": "Don Corleone",
"actor": {
"actorId": 2
}
}"
2) อย่างน้อยฉันสามารถดึงอะไรแบบนี้ได้ด้วยพารามิเตอร์ replacer ของ JSON.stringify() ไหม?
{
"fullName": "Don Corleone",
"actor": {
"actorId": 2,
"name": "Marlon",
"surname": "Brando",
"description": "Marlon Brando is widely considered the greatest movie actor of all time... ",
"heroList": [],
"photo": "C:\\projects\\files\\actor\\1532955376934.png"
},
}"
เมื่อฉันใช้งานแบบนี้ มันโอเค:
JSON.stringify(hero, ['fullName']) ผลลัพธ์ -> "{\"fullName\":\"Don Corleone\"}"
แต่สิ่งนี้ :
JSON.stringify(hero, ['fullName', 'actor']) ผลลัพธ์ -> "{\"fullName\":\"Don Corleone\",\"actor\":{}}"
ทำไมนักแสดงถึงมีคุณสมบัติเป็นค่าว่าง?
คำตอบ #1
JSON.stringify ต้องการให้คุณส่งข้อมูลทั้งหมดที่คุณต้องการให้ส่งกลับ 'actor' เพียงอย่างเดียวไม่เพียงพอ
คุณต้องการ:
JSON.stringify(hero, ['fullName', 'actor', 'actorId'])แก้ไข
ดังนั้นฉันจึงได้ทำการทดสอบบ้าง ฉันอยากรู้ว่าจะเกิดอะไรขึ้นถ้า actorId มีอยู่ในวัตถุแม่ด้วย และผลลัพธ์ก็เป็นดังนี้
ฟิลด์ actorId ทั้งสองจะถูกส่งกลับโดย JSON.stringify() ในกรณีที่ actorId มีอยู่ภายในออบเจ็กต์ actor และภายในออบเจ็กต์หลัก หากคุณไม่ต้องการให้เป็นเช่นนี้ คุณจะต้องสร้างฟังก์ชันที่ซับซ้อนมากขึ้นสำหรับความต้องการของคุณและส่งผ่านไปยัง JSON.stringify() ตามที่เอกสารระบุไว้ที่นี่
นี่คือตัวอย่างบางส่วน:
var json = {
key1: "Value for key1 in parent",
key2: {
key3: "Value for key3 in child",
key4: "Value for key4 in child"
},
key4: "Value for key4 in parent"
}
var out1 = JSON.stringify(json, ['key1', 'key3'])
/*
out1 = {
key1: "Value for key1 in parent"
} // No key3!
*/
var out2 = JSON.stringify(json, ['key1', 'key2', 'key3'])
/*
out2 = {
key1: "Value for key1 in parent",
key2: {
key3: "Value for key3 in child"
}
}
*/
var out3 = JSON.stringify(json, ['key1', 'key2', 'key3', 'key4'])
/*
out3 = {
key1: "Value for key1 in parent",
key2: {
key3: "Value for key3 in child",
key4: "Value for key4 in child" // Both key4 returned
},
key4: "Value for key4 in parent" // Both key4 returned
}
*/
คำตอบ #2
เราสามารถทำได้อย่างง่ายดายโดยใช้ Spread ใน object literals
ลองอ่านวัตถุเหมือน
const obj={yourObj.fullname, yourObj.actor} const yourAnswer=JSON.stringify(obj)
คุณสามารถอ้างอิงลิงก์ Spread_syntax ได้
เนื่องจาก JSON.stringify ช้ามากสำหรับอ็อบเจ็กต์ขนาดใหญ่ ลองใช้วิธีข้างต้น
