반응형
underscore 라이브러리
배열 정렬과 같은 기본적인 유틸리티 기능을 모아놓은 underscore 라이브러리
:: underscore 라이브러리는 모든 웹 브라우저에서 작동하므로, 인터넷 익스플로러 8 이하의 웹 브라우저에서 ECMAScript 5에 추가된 메서드를 사용할 수 없을 때 보완하기 위한 목적으로도 활용할 수 있습니다.
underscore 라이브러리 활용하기
Development Version ( 개발버전 ) or Production Version ( 배포버전 ) 다운로드 -> underscore 라이브러리 경로 지정해주기
underscore 라이브러리 활용 예
var students = [
{ name: 'shiro', korean: 87, english: 99, math: 100, science: 87 },
{ name: 'AAA', korean: 65, english: 36, math: 55, science: 100 },
{ name: 'BBB', korean: 87, english: 78, math: 0, science: 11 },
{ name: 'CCC', korean: 87, english: 12, math: 55, science: 92 },
{ name: 'DDD', korean: 99, english: 55, math: 96, science: 77 },
{ name: 'SSS', korean: 78, english: 89, math: 100, science: 91 },
{ name: 'ADS', korean: 99, english: 99, math: 45, science: 90 }
];
var sortByScience = _.sortBy(students, function(item) {
return item.science
}).slice(0, 3);
console.log(JSON.stringify(sortByScience, null, 2));
/*
[
{
"name": "BBB",
"korean": 87,
"english": 78,
"math": 0,
"science": 11
},
{
"name": "DDD",
"korean": 99,
"english": 55,
"math": 96,
"science": 77
},
{
"name": "shiro",
"korean": 87,
"english": 99,
"math": 100,
"science": 87
}
]
*/
var sortByAverage = _.sortBy(students, function(item) {
return (item.korean + item.english + item.math + item.science) / 4
}).slice(0, 3);
console.log(JSON.stringify(sortByAverage, null, 2));
/*
[
{
"name": "BBB",
"korean": 87,
"english": 78,
"math": 0,
"science": 11
},
{
"name": "CCC",
"korean": 87,
"english": 12,
"math": 55,
"science": 92
},
{
"name": "AAA",
"korean": 65,
"english": 36,
"math": 55,
"science": 100
}
]
*/
// 과학성적이 가장 높은 학생 추출하기
var scienceTop = _.max(students, function(item) { return item.science})
console.log(JSON.stringify(scienceTop, null, 2));
/*
{
"name": "AAA",
"korean": 65,
"english": 36,
"math": 55,
"science": 100
}
*/
반응형
'JavaScript | TypeScript > Javascript 시작하기' 카테고리의 다른 글
[ Javascript ] window 객체의 기본 메서드 (0) | 2022.06.27 |
---|---|
[ Javascript ] 브라우저 객체 모델 (0) | 2022.06.27 |
[ Javascript ] 화살표 함수를 사용한 Array 객체의 메서드 활용 (1) | 2022.06.23 |
[ Javascript ] JSON 객체 (0) | 2022.06.23 |
[ Javascript ] ECMAScript 5 Array 객체 (0) | 2022.06.22 |