다음과 같은 객체 배열이 있다고 가정해 보겠습니다. -
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 함수를 작성해야 합니다. 이 함수는 모든 (동일한) 객체가 위치 속성을 기반으로 함께 그룹화되는 새로운 객체 배열을 준비해야 합니다.
그리고 개체는 개체의 원래 배열에 나타난 횟수를 포함하는 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 }
]