프로그래밍/Javascript

[JAVASCRIPT/자바스크립트] lodash 라이브러리 sortBy

Rolen 2022. 11. 19. 00:22
const _ = require('lodash'); // lodash 라이브러리 호출 

let arrayB = [{
    name: '고구마',
    price: 1000
}, {
    name: '감자',
    price: 500
}, {
    name: '바나나',
    price: 400
}];

sortedB = _.sortBy(arrayB, (a) => a.price);
console.log("가격 오름차순", sortedB);
console.log("원본 데이터", arrayB); // 원본 데이터가 손상(변형)되지 않는다.
// 이름을 기준으로 오름차순 정렬하시오.
sortedN = _.sortBy(arrayB, (a) => a.name);
console.log("이름 오름차순", sortedN);
728x90