最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import and use _.sortBy from lodash - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use _.sortBy method from lodash by making a code like this :

import _sortBy from 'lodash';
var questions = [.......some array....];
questions = _sortBy(questions, 'position');

console.log(questions);

The content of console.log(question) is empty. I don't know why, I think I don't know how to load _.sortBy from lodash. I have tried this :

questions = _sortBy._.sortBy(questions, 'position')

or

questions = _.sortBy(questions, 'position')

But there is an error "bundle.js:45504 Uncaught TypeError: Cannot read property 'sortBy' of undefined" or "Uncaught TypeError: (0 , _jquery2.default)(...).sortable is not a function". Also I have read this page :

But I have no idea how to load and use this _.sortBy method. Please help..:)

Note: Sorry for the stupid question if this question is so easy

I'm trying to use _.sortBy method from lodash by making a code like this :

import _sortBy from 'lodash';
var questions = [.......some array....];
questions = _sortBy(questions, 'position');

console.log(questions);

The content of console.log(question) is empty. I don't know why, I think I don't know how to load _.sortBy from lodash. I have tried this :

questions = _sortBy._.sortBy(questions, 'position')

or

questions = _.sortBy(questions, 'position')

But there is an error "bundle.js:45504 Uncaught TypeError: Cannot read property 'sortBy' of undefined" or "Uncaught TypeError: (0 , _jquery2.default)(...).sortable is not a function". Also I have read this page : https://lodash.com/docs#sortBy

But I have no idea how to load and use this _.sortBy method. Please help..:)

Note: Sorry for the stupid question if this question is so easy

Share Improve this question edited Aug 9, 2016 at 7:41 Maryadi Poipo asked Aug 9, 2016 at 7:32 Maryadi PoipoMaryadi Poipo 1,4789 gold badges32 silver badges55 bronze badges 5
  • import _sortBy from 'lodash'; here you are importing lodash and not a module in it. it should be import _ from 'lodash'; and then you can do _.sortBy – Rajesh Commented Aug 9, 2016 at 7:36
  • If I just use _.sortBy, there is an error show up "Uncaught TypeError: (0 , _jquery2.default)(...).sortable is not a function", seemed that _.sortBy is not loaded... Any clue Mr. Rajesh..?? :) – Maryadi Poipo Commented Aug 9, 2016 at 7:40
  • Try checking what you are getting in _sortBy. – Rajesh Commented Aug 9, 2016 at 7:41
  • mmm... Okie... Lemme take a moment.. :) – Maryadi Poipo Commented Aug 9, 2016 at 7:43
  • Ooowh... It seems cool.. :D Thanks Mr. Rajesh.. ;) – Maryadi Poipo Commented Aug 9, 2016 at 7:50
Add a comment  | 

2 Answers 2

Reset to default 10

In your case, you have two options :

  • you can import the entire collection : import _ from 'lodash'; Then you will be able to use it like this : _.sortBy()

  • However, you can also import specific function. So rather import whole lodash collection, it's better just import lodash's function, especially when you work in production.

So you can do : import sortBy from 'lodash/sortBy';

Then you will be able to use it like this : sortBy()

you can import specific item using curly braces.

how it works ?

_ is not exported as default so using like below will throw error.

import _ from lodash

however you can import everything from lodash in _ using below code

import * as _ from 'lodash';

but if you want to import specific functions or anything which is exported you can use it like below example

import {sortBy} from 'lodash';

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];

sortBy(users, function(o) { return o.user; });
发布评论

评论列表(0)

  1. 暂无评论