-
JS 자세히 알아보기 (lodash)JavaScript/JavaScript 2021. 6. 7. 00:41
lodash
import _ from 'lodash' // From `node_modules`! const usersA = [ { userId: '1', name: 'Yoo' }, { userId: '2', name: 'Lee' } ] const usersB = [ { userId: '1', name: 'Yoo' }, { userId: '3', name: 'Kim' } ] // 배열을 병합 const usersC = usersA.concat(usersB) // 0: {userId: "1", name: "Yoo"} // 1: {userId: "2", name: "Lee"} // 2: {userId: "1", name: "Yoo"} // 3: {userId: "3", name: "Kim"} console.log(usersC) // _.uniqBy(배열 데이터, 속성) // 해당 배열 데이터 내에서 속성의 고유한 값만 정리해서 반환해준다 // 0: {userId: "1", name: "Yoo"} // 1: {userId: "2", name: "Lee"} // 2: {userId: "3", name: "Kim"} console.log(_.uniqBy(usersC, 'userId')) // _.unionBy(배열 데이터1, 배열 데이터2, 속성) // 배열을 합쳐준다 (속성의 고유한 값만 남김 ) // 0: {userId: "1", name: "Yoo"} // 1: {userId: "2", name: "Lee"} // 2: {userId: "3", name: "Kim"} const usersD = _.unionBy(usersA, usersB, 'userId') console.log(usersD)
import _ from 'lodash' // From `node_modules`! const users = [ { userId: '1', name: 'Yoo' }, { userId: '2', name: 'Lee' }, { userId: '3', name: 'Kim' }, { userId: '4', name: 'Park' } ] // _.find(배열 데이터, 찾을 값) // 해당 배열에서 찾은 값의 데이터를 반환 const foundUser = _.find(users, { name: 'Lee' }) // {userId: "2", name: "Lee"} console.log(foundUser) // _.foundUserIndex(배열 데이터, 찾을 값) // 해당 배열에서 찾은 값의 인덱스를 반환 const foundUserIndex = _.findIndex(users, { name: 'Kim' }) // 2 console.log(foundUserIndex) // _.remove(배열 데이터, 지울 값) // 해당 배열에서 찾은 값의 데이터를 지움 _.remove(users, { name: 'Yoo' }) // 0: {userId: "2", name: "Lee"} // 1: {userId: "3", name: "Kim"} // 2: {userId: "4", name: "Park"} console.log(users)
참조
Lodash
_.defaults({ 'a': 1 }, { 'a': 3, 'b': 2 });_.partition([1, 2, 3, 4], n => n % 2);DownloadLodash is released under the MIT license & supports modern environments. Review the build differences & pick one that’s right for you.InstallationIn
lodash.com
'JavaScript > JavaScript' 카테고리의 다른 글
JS 자세히 알아보기 (Storage) (0) 2021.06.09 JS 자세히 알아보기 (JSON) (0) 2021.06.08 JS 자세히 알아보기 (가져오기, 내보내기) (0) 2021.06.07 JS 자세히 알아보기 (전개 연산자, 데이터 불변성, 복사) (0) 2021.06.06 JS 자세히 알아보기 (객체 데이터) (0) 2021.06.06