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

จัดกลุ่มออบเจ็กต์ JSON โดยสองคุณสมบัติและนับจำนวนใน JavaScript

สมมติว่ามีอาร์เรย์ของออบเจ็กต์ดังนี้:

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 = []) => {
  // สร้าง 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 }
]