JavaScriptはJSONオブジェクトを2つのプロパティでグループ化し、カウントします
このようなオブジェクトの配列があるとします-
const arr = [ {"location":"Kirrawee","identity_long":"student"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"worker"}, {"location":"Sutherland","identity_long":"student"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Sutherland","identity_long":"worker"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Miranda","identity_long":"resident"}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"student"}, {"location":"Miranda","identity_long":""}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"resident"} ];
そのようなオブジェクトの配列を1つ取り込むJavaScript関数を作成する必要があります。関数は、すべての(同一の)オブジェクトがlocationプロパティに基づいてグループ化されたオブジェクトの新しい配列を準備する必要があります。
また、オブジェクトには、オブジェクトの元の配列に出現した回数を含むcountプロパティを割り当てる必要があります。
したがって、上記の配列の場合、出力は次のようになります-
const output = [ {"location":"Kirrawee","identity":"student","count":1}, {"location":"Kirrawee","identity":"visitor","count":2}, {"location":"Kirrawee","identity":"worker","count":1}, {"location":"Sutherland","identity":"student","count":1}, {"location":"Sutherland","identity":"resident","count":2}, {"location":"Sutherland","identity":"worker","count":1}, {"location":"Miranda","identity":"resident","count":2}, {"location":"Miranda","identity":"worker","count":2}, {"location":"Miranda","identity":"student","count":1} ];
例
このためのコードは-
になりますconst arr = [ {"location":"Kirrawee","identity_long":"student"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"worker"}, {"location":"Sutherland","identity_long":"student"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Sutherland","identity_long":"worker"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Miranda","identity_long":"resident"}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"student"}, {"location":"Miranda","identity_long":""}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"resident"} ]; const groupArray = (arr = []) => { // create map let map = new Map() for (let i = 0; i < arr.length; i++) { const s = JSON.stringify(arr[i]); if (!map.has(s)) { map.set(s, { location: arr[i].location, identity: arr[i].identity_long, count: 1, }); } else { map.get(s).count++; } } const res = Array.from(map.values()) return res; }; console.log(groupArray(arr));
出力
そして、コンソールの出力は-
になります[ { location: 'Kirrawee', identity: 'student', count: 1 }, { location: 'Kirrawee', identity: 'visitor', count: 2 }, { location: 'Kirrawee', identity: 'worker', count: 1 }, { location: 'Sutherland', identity: 'student', count: 1 }, { location: 'Sutherland', identity: 'resident', count: 2 }, { location: 'Sutherland', identity: 'worker', count: 1 }, { location: 'Miranda', identity: 'resident', count: 2 }, { location: 'Miranda', identity: 'worker', count: 2 }, { location: 'Miranda', identity: 'student', count: 1 }, { location: 'Miranda', identity: '', count: 1 } ]
-
JavaScriptでオブジェクトを作成してそのプロパティにアクセスするにはどうすればよいですか?
以下は、オブジェクトを作成し、JavaScriptでそのプロパティにアクセスするコードです- 例 <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <st
-
JavaScriptで既存のオブジェクトにプロパティとメソッドを追加するにはどうすればよいですか?
以下は、JavaScriptの既存のオブジェクトにプロパティとメソッドを追加するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <