JavaScriptのJSONオブジェクトの各エントリに一意の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" } ] } ] }] }];
そのような配列を1つ取り込むJavaScript関数を作成する必要があります。次に、関数は「タイトル」フィールドを持つすべてのオブジェクトに「id」フィールドを追加する必要があります。
「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));
出力
そして、コンソールの出力は-
になります[ { "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" } ] } ] } ] } ]
-
JSONテキストをJavaScriptJSONオブジェクトに変換する方法は?
JSON parse()メソッドは、JSONテキストをJavaScriptオブジェクトに変換するために使用されます。 以下は、JSONテキストをJavaScriptJSONオブジェクトに変換するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scal
-
JavaScriptでオブジェクトごとに一意のIDを作成するにはどうすればよいですか?
以下は、オブジェクトごとに一意のIDを作成するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>