Grouping JSON by values : JSON 값으로 그룹핑 분류

Assume , the JSON output is

outJSON= 
 [ {
      team: "TeamA",
      name: "Ahmed",
      field3:"val3"
  }, 
{
      team: "TeamB",
      name: "Ahmed",
      field3:"val43"
  }, 
{
      team: "TeamA",
      name: "Ahmed",
      field3:"val55"
  }, 


]

Then see the groupBy function in the DEMO below:


DEMO :

 

outJSON= [ {team: "TeamA",name: "Ahmed",field3:"val3"}, {team: "TeamB",name: "Ahmed",field3:"val43"}, {team: "TeamA",name: "Ahmed",field3:"val55"} ]

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
var groubedByTeam=groupBy(outJSON, 'team')
console.log(groubedByTeam);

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Then , if you want to loop through categories (teams), get all categories in array :

Object.keys(groubedByTeam) // return ["TeamA","TeamB"]

then :

  Object.keys(groubedByTeam).forEach(function(category){

       console.log(`Team ${category} has ${groubedByTeam[category].length} members : `);
        groubedByTeam[category].forEach(function(memb,i){
              console.log(`---->${i+1}. ${memb.name}.`)
       })
  }); 

[출처] https://stackoverflow.com/questions/38575721/grouping-json-by-values

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
263 서버 사이드 렌더링 그리고 클라이언트 사이드 렌더링 file 졸리운_곰 2018.11.02 836
262 AWS Certified Solutions Architect - Associate 2018 후기 file 졸리운_곰 2018.10.31 701
261 AWS 자격증 준비하기 file 졸리운_곰 2018.10.31 1037
260 AWS 자격증 (아키텍트) 준비요령 file 졸리운_곰 2018.10.31 822
259 AWS Certified Solutions Architect – Associate Exam 시험 후기 file 졸리운_곰 2018.10.31 843
258 underscore.js로 편해지자 졸리운_곰 2018.10.16 595
257 Express 앱용 프로세스 관리자 졸리운_곰 2018.10.16 655
256 [vue.js] eclipse에서 vue.js 시작하기 file 졸리운_곰 2018.09.18 1120
255 카드형 디자인/갤러리/리스트 코딩하기. file 졸리운_곰 2018.09.06 498
254 jquery json에서 null 값 삭제 졸리운_곰 2018.09.05 342
253 jquery .append(Html). 에서 claas 적용 졸리운_곰 2018.09.05 256
252 JavaScript / Method / .split() – 문자열 분할하는 속성 file 졸리운_곰 2018.09.05 414
251 Javascript - 문자열에서 큰 따옴표를 제거하고 싶습니다. 졸리운_곰 2018.09.05 497
250 [JavaScript] Object.keys, json Object 반복문 돌리기 for...in 문 졸리운_곰 2018.09.05 678
249 javascript JSON Object의 key 개수(length) 구하기. 졸리운_곰 2018.09.05 515
248 jQuery Mobile Form Example file 졸리운_곰 2018.09.04 447
247 treat json objects value as integer : javascript JSON 값을 정수로 변경 졸리운_곰 2018.09.03 287
246 Check if a key exists inside a json object : JSON 키값이 있는지 조사 졸리운_곰 2018.09.03 478
» Grouping JSON by values : JSON 값으로 그룹핑 분류 졸리운_곰 2018.09.01 573
244 간단한 워드프레스 숏코드 만드는 방법 총정리 졸리운_곰 2018.09.01 777
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED