Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

เพิ่ม ID ที่ไม่ซ้ำสำหรับแต่ละรายการในอ็อบเจ็กต์ JSON ใน JavaScript

สมมติว่าเรามีอาร์เรย์ที่อธิบายดังนี้:

const arr = [
  {
    "Arts": [
      {
        "Performing arts": [
          {
            "Music": [
              {
                "title": "Accompanying"
              },
              {
                "title": "Chamber music"
              },
              {
                "title": "Church music"
              },
              {
                "Conducting": [
                  {
                    "title": "Choral conducting"
                  },
                  {
                    "title": "Orchestral conducting"
                  },
                  {
                    "title": "Wind ensemble conducting"
                  }
                ]
              },
              {
                "title": "Early music"
              },
              {
                "title": "Jazz studies"
              },
              {
                "title": "Musical composition"
              },
              {
                "title": "Music education"
              },
              {
                "title": "Music history"
              },
              {
                "Musicology": [
                  {
                    "title": "Historical musicology"
                  },
                  {
                    "title": "Systematic musicology"
                  }
                ]
              },
              {
                "title": "Ethnomusicology"
              },
              {
                "title": "Music theory"
              },
              {
                "title": "Orchestral studies"
              },
              {
                "Organology": [
                  {
                    "title": "Organ and historical keyboards"
                  },
                  {
                    "title": "Piano"
                  },
                  {
                    "title": "Strings, harp, oud, and guitar"
                  },
                  {
                    "title": "Singing"
                  },
                  {
                    "title": "Strings, harp, oud, and guitar"
                  }
                ]
              },
              {
                "title": "Recording"
              }
            ]
          },
          {
            "Dance": [
              {
                "title": "Choreography"
              },
              {
                "title": "Dance notation"
              },
              {
                "title": "Ethnochoreology"
              },
              {
                "title": "History of dance"
              }
            ]
          },
          {
            "Television": [
              {
                "title": "Television studies"
              }
            ]
          },
          {
            "Theatre": [
              {
                "title": "Acting"
              },
              {
                "title": "Directing"
              },
              {
                "title": "Dramaturgy"
              },
              {
                "title": "History"
              },
              {
                "title": "Musical theatre"
              },
              {
                "title": "Playwrighting"
              },
              {
                "title": "Puppetry"
              }
            ]
          }
        ]
      }
    }
  }
}];

เราต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์ดังกล่าว ฟังก์ชันควรเพิ่มฟิลด์ "id" ในอ็อบเจ็กต์ทั้งหมดที่มีฟิลด์ "title" ค่าของคุณสมบัติ "id" ไม่ได้สำคัญมาก (อาจเป็นค่าไม่ซ้ำใดก็ได้) สิ่งที่สำคัญกว่าคืออ็อบเจ็กต์ทั้งหมดที่มีคุณสมบัติ "title" ต้องมีคุณสมบัติ "id" เราต้องทำสิ่งนี้โดยไม่สร้างสำเนาของอาร์เรย์จริง

ตัวอย่าง

โค้ดสำหรับทำสิ่งนี้คือ:

const arr = [
  {
    "Arts": [
      {
        "Performing arts": [
          {
            "Music": [
              {
                "title": "Accompanying"
              },
              {
                "title": "Chamber music"
              },
              {
                "title": "Church music"
              },
              {
                "Conducting": [
                  {
                    "title": "Choral conducting"
                  },
                  {
                    "title": "Orchestral conducting"
                  },
                  {
                    "title": "Wind ensemble conducting"
                  }
                ]
              },
              {
                "title": "Early music"
              },
              {
                "title": "Jazz studies"
              },
              {
                "title": "Musical composition"
              },
              {
                "title": "Music education"
              },
              {
                "title": "Music history"
              },
              {
                "Musicology": [
                  {
                    "title": "Historical musicology"
                  },
                  {
                    "title": "Systematic musicology"
                  }
                ]
              },
              {
                "title": "Ethnomusicology"
              },
              {
                "title": "Music theory"
              },
              {
                "title": "Orchestral studies"
              },
              {
                "Organology": [
                  {
                    "title": "Organ and historical keyboards"
                  },
                  {
                    "title": "Piano"
                  },
                  {
                    "title": "Strings, harp, oud, and guitar"
                  },
                  {
                    "title": "Singing"
                  },
                  {
                    "title": "Strings, harp, oud, and guitar"
                  }
                ]
              },
              {
                "title": "Recording"
              }
            ]
          },
          {
            "Dance": [
              {
                "title": "Choreography"
              },
              {
                "title": "Dance notation"
              },
              {
                "title": "Ethnochoreology"
              },
              {
                "title": "History of dance"
              }
            ]
          },
          {
            "Television": [
              {
                "title": "Television studies"
              }
            ]
          },
          {
            "Theatre": [
              {
                "title": "Acting"
              },
              {
                "title": "Directing"
              },
              {
                "title": "Dramaturgy"
              },
              {
                "title": "History"
              },
              {
                "title": "Musical theatre"
              },
              {
                "title": "Playwrighting"
              },
              {
                "title": "Puppetry"
              }
            ]
          }
        ]
      }
    }
  }
}];
const addId = (id = 1) => {
  return function recur(obj) {
    if ('title' in obj) {
      obj.id = id++;
    };
    Object.keys(obj).forEach(el => {
      Array.isArray(obj[el]) && obj[el].forEach(recur);
    });
  };
}
const mapId = arr => {
  arr.forEach(addId);
}
mapId(arr);
console.log(JSON.stringify(arr, undefined, 4));

ผลลัพธ์

ผลลัพธ์ในคอนโซลจะเป็นดังนี้:

[ ... (JSON structure with id added to each title object) ... ]