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 |2 Answers
Reset to default 10In 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; });
import _sortBy from 'lodash';
here you are importing lodash and not a module in it. it should beimport _ from 'lodash';
and then you can do_.sortBy
– Rajesh Commented Aug 9, 2016 at 7:36_sortBy
. – Rajesh Commented Aug 9, 2016 at 7:41