मान लीजिए, हमारे पास इस तरह की वस्तुओं की एक सरणी है -
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 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 } ]