lodash

Lodash module not found + webpack

吃可爱长大的小学妹 提交于 2020-07-22 05:18:36
问题 I try use the library ladash but I´m getting the next error: Module not found: Error: Can't resolve 'lodash' in I import the library as: import _ from 'lodash'; This is the webpack config: { loader: 'babel-loader', test: /\.js$/, exclude: /node_modules/, options: { plugins: ['lodash'], presets: [['env', { modules: false, targets: { node: 4 } }]] } } ...... ...... plugins: [ new LodashModuleReplacementPlugin, new webpack.optimize.UglifyJsPlugin ] and this is my package json dependencies: 回答1:

sort array of numeric strings with Lodash

感情迁移 提交于 2020-07-18 21:43:13
问题 _.sortBy(arrData, "rhid"); This code sorts array but as the values of field "rhid" are strings the order is messed up. How can i sort as if "rhid" where int field. Thanks 回答1: sortBy can be used with a function instead of a property name. _.sortBy(arrData, function (obj) { return parseInt(obj.rhid, 10); }); 来源: https://stackoverflow.com/questions/34724034/sort-array-of-numeric-strings-with-lodash

How to find objects in array where inner array might include other objects by using lodash

核能气质少年 提交于 2020-07-09 11:27:50
问题 I have the following arrays: const tasks = [ {id: 0, name: 'a', tags: [{id: 0, name: 'q'}, {id: 1, name: 'w'}]}, {id: 1, name: 'b', tags: [{id: 2, name: 'e'}, {id: 4, name: 't'}, {id: 11, name: 's'}]}, {id: 2, name: 'c', tags: []}, {id: 3, name: 'd', tags: [{id: 0, name: 'q'}, {id: 3, name: 'r'}, {id: 7, name: 'i'}]}, {id: 6, name: 'g', tags: [{id: 7, name: 'i'}, {id: 4, name: 't'}]}, ] const tags = [ {id: 0, name: 'q'}, {id: 1, name: 'w'}, {id: 2, name: 'e'}, {id: 3, name: 'r'}, {id: 4, name

Sort object by value with lodash

牧云@^-^@ 提交于 2020-07-08 10:39:16
问题 I have an object that looks like this: var unsorted = {a: 15, b: 1,c: 2, d: 4, e: 3, f: 6, g: 1} And I am interested in sorted it in descending order with lodash, so the expected result should be: var sortd = {b: 1, g: 1,c: 2, e: 3, d: 4, f: 6, a: 15} I have tried using lodash orderBy but it gives an array of just the values sorted. _.orderBy(tempCount, Number, ['desc']) //Result [1,1,2,3,4,6,15] 回答1: You could build a new object with the sorted key/value pairs. The actual standard or the

Sort object by value with lodash

守給你的承諾、 提交于 2020-07-08 10:38:38
问题 I have an object that looks like this: var unsorted = {a: 15, b: 1,c: 2, d: 4, e: 3, f: 6, g: 1} And I am interested in sorted it in descending order with lodash, so the expected result should be: var sortd = {b: 1, g: 1,c: 2, e: 3, d: 4, f: 6, a: 15} I have tried using lodash orderBy but it gives an array of just the values sorted. _.orderBy(tempCount, Number, ['desc']) //Result [1,1,2,3,4,6,15] 回答1: You could build a new object with the sorted key/value pairs. The actual standard or the

Sort object by value with lodash

只愿长相守 提交于 2020-07-08 10:36:51
问题 I have an object that looks like this: var unsorted = {a: 15, b: 1,c: 2, d: 4, e: 3, f: 6, g: 1} And I am interested in sorted it in descending order with lodash, so the expected result should be: var sortd = {b: 1, g: 1,c: 2, e: 3, d: 4, f: 6, a: 15} I have tried using lodash orderBy but it gives an array of just the values sorted. _.orderBy(tempCount, Number, ['desc']) //Result [1,1,2,3,4,6,15] 回答1: You could build a new object with the sorted key/value pairs. The actual standard or the

How can I remove object from array, with Lodash?

你离开我真会死。 提交于 2020-07-05 07:35:27
问题 I'm trying to remove an object from an array using Lodash. In server.js (using NodeJS): var lodash = require('lodash')(); var rooms = [ { channel: 'room-a', name: 'test' }, { channel: 'room-b', name: 'test' } ] I tried with two commands and it did not work: var result = lodash.find(rooms, {channel: 'room-a', name:'test'}); var result = lodash.pull(rooms, lodash.find(rooms, {channel: 'room-a', name:'test'})); Here's the output of console.log(result) : LodashWrapper { __wrapped__: undefined, _

Remove original and duplicate from an array of objects - JS

一世执手 提交于 2020-07-03 13:42:49
问题 I have an array of objects. const arr = [ { title: "sky", artist: "Jon", id: 1 }, { title: "rain", artist: "Paul", id: 2 }, { title: "sky", artist: "Jon", id: 1 } ]; I would like to remove all duplicates from the array based on id. The final result should be [{ title: "rain", artist: "Paul", id: 2 }] If the array is const arr = [ { title: "sky", artist: "Jon", id: 1 }, { title: "rain", artist: "Paul", id: 2 }, { title: "sky", artist: "Jon", id: 1 }, { title: "rain", artist: "Paul", id: 2 }, ]

A better way to trim all elements in an object recursively?

江枫思渺然 提交于 2020-06-27 18:48:11
问题 If I have an object like, const obj = { field: { subfield: { innerObj: { a: ' asdasd asdas . ' }, innerArr: [' s ', ' ssad . '], innerArrObj: [ { b: ' adsad ' } ], } } } I've come up with something like this, const trimFields = (data) => _.mapValues(data, (element, k) => _.isArray(element) ? element.map((value) => _.isObject(value) ? trimFields(value) : trimText(value) ) : _.isObject(element) ? trimFields(element) : trimText(element) ); But I'm wondering if there is a better / more efficient

Testing debounced method in React with Enzyme

送分小仙女□ 提交于 2020-06-27 11:04:54
问题 //Component import _ from 'lodash'; constructor(props) { super(props); this.onScroll = _.debounce(::this.onScroll, 100); } onScroll() { //some code } //Test it('onScroll', () => { const component = shallow(<Component />); component.instance().onScroll(); //Dosn't call method }) I use enzyme for render component, and lodash for debounce. How to call component.instance().onScroll() ? 回答1: TL;DR; use lolex to advance in time while mocking timers for testing _.debounce I was ready to describe