Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> JavaScript

JavaScript में JSON ऑब्जेक्ट को दो गुणों द्वारा ग्रुप करना और काउंट करना


मान लीजिए, हमारे पास ऑब्जेक्ट्स की एक सरणी है जैसे -

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"} ];

हमें एक 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 = []) => {    // मैप बनाएं    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 } ]

  1. JavaScript ऑब्जेक्ट प्रॉपर्टीज JavaScript ऑब्जेक्ट प्रॉपर्टीज

    JavaScript में प्रॉपर्टीज वे वैल्यू होती हैं जो किसी ऑब्जेक्ट से जुड़ी होती हैं। JavaScript में ऑब्जेक्ट प्रॉपर्टीज को लागू करने का कोड निम्न है -उदाहरण<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" cont

  1. JavaScript में ऑब्जेक्ट कैसे बनाएं और उसकी प्रॉपर्टीज एक्सेस करें? JavaScript में ऑब्जेक्ट कैसे बनाएं और उसकी प्रॉपर्टीज एक्सेस करें?

    JavaScript में ऑब्जेक्ट बनाने और उसकी प्रॉपर्टीज एक्सेस करने का कोड निम्न है -उदाहरण<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <tit

  1. जावास्क्रिप्ट में मौजूदा ऑब्जेक्ट में प्रॉपर्टीज और मेथड्स कैसे जोड़ें? जावास्क्रिप्ट में मौजूदा ऑब्जेक्ट में प्रॉपर्टीज और मेथड्स कैसे जोड़ें?

    जावास्क्रिप्ट में मौजूदा ऑब्जेक्ट में प्रॉपर्टीज और मेथड्स जोड़ने के लिए निम्नलिखित कोड है: उदाहरण <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"