I'm using Laravel 5.5. I've run npm install
and npm run dev
and generally I have a good working environment with running Javascript, but I've been caught out while trying to use lodash's remove()
function to remove an element from an array.
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
return n % 2 == 0;
});
The above code produces the following error:
TypeError: _.remove is not a function
Lodash is loaded in resources/assets/js/bootstrap.js
.
window._ = require('lodash');
Other lodash methods works fine.
_.each([1, 2], function(n) {
console.log(n);
});
Any idea why I can't use .remove()
?
The problem was that I'm also using Laravel Spark. Spark does not use resources/assets/js/bootstrap.js
and loads its own bootstrap at vendor/laravel/spark/resources/assets/js/spark-bootstrap.js
instead.
Spark's bootstrap actually requires the underscore package instead of lodash, hence the confusion.
window._ = require('underscore');