lodash

Lo-Dash sortBy array of dates in string format

雨燕双飞 提交于 2019-12-05 17:49:12
问题 I would like to know why lodash doesn't sort array of dates in string format as compared with plain javascript sort() . Is it expected behaviour or a bug? array = ["2014-11-11", "2014-11-12", null, "2014-11-01", null, null, "2014-11-05"] _.sortBy(array); // ["2014-11-11", "2014-11-12", null, "2014-11-01", null, null, "2014-11-05"] _.sortBy(array, function(value) {return new Date(value);}); // [null, null, null, "2014-11-01", "2014-11-05", "2014-11-11", "2014-11-12"] array.sort() // ["2014-11

Object.keys equivalent lodash method

情到浓时终转凉″ 提交于 2019-12-05 15:18:43
问题 I am new to loadash, I am trying to learn good ways to manipulate java script object. Is there a equivalent loadash method for : Object.keys({ "tab1": "1" , tab2: "2"})[0]; Object.keys({ "tab1": "1" , tab2: "2"})[2]; to get list values? And also if there are easy and good ways to use lodash and any articles that I can go through. 回答1: _.keys should do the trick. _.keys(object) Creates an array of the own enumerable property names of object . Example: console.log(_.keys({ "tab1": "1" , tab2:

Lodash: is it possible to use map with async functions?

旧城冷巷雨未停 提交于 2019-12-05 15:02:42
问题 Consider this code const response = await fetch('<my url>'); const responseJson = await response.json(); responseJson = _.sortBy(responseJson, "number"); responseJson[0] = await addEnabledProperty(responseJson[0]); What addEnabledProperty does is to extend the object adding an enabled property, but this is not important. The function itself works well async function addEnabledProperty (channel){ const channelId = channel.id; const stored_status = await AsyncStorage.getItem(`ChannelIsEnabled:$

Lodash _.hasIntersection?

北战南征 提交于 2019-12-05 14:39:17
I want to know if two or more arrays have common items, but I don't care what those items are. I know lodash has an _.intersection method, but I don't need it to run through every single item of each array. Instead, I need something like a _.hasIntersection method that will stop looking in an array once it finds the first common occurrence. Does lodash have something like that? This approach lets you efficiently search for an intersection in an arbitrary number of arrays. function hasIntersection() { var collections = _.rest(arguments); return _.some(_.first(arguments), function(item) { return

How to remove a property from nested javascript objects any level deep?

岁酱吖の 提交于 2019-12-05 14:30:55
Let's say I have nested objects, like: var obj = { "items":[ { "name":"Item 1", "value": "500", "options": [{...},{...}] }, { "name":"Item 2", "value": "300", "options": [{...},{...}] } ], "name": "Category", "options": [{...},{...}] }; I want to remove the options property from any level deep from all the objects. Objects can be nested within objects, and arrays as well. We're currently using Lodash in the project, but I'm curious about any solutions. There is no straight forward way to achieve this, however you can use this below function to remove a key from JSON. function filterObject(obj,

How to replace an object in object array in javascript (lodash)

▼魔方 西西 提交于 2019-12-05 14:13:17
I have following object array: var arr = [ { id : "a1", guid : "sdfsfd", ... value : "abc", status: false }, { id : "a2", guid : "sdfsfd", ... value : "def", status: true }, ... ] I have this object: var obj = { id : "a1", guid : "sdfsfd", ... value : "xyz", status : true } I need to replace the object in the array with this object where the "id" is same. So the resulting array will be: var arr = [ { id : "a1", guid : "sdfsfd", ... value : "xyz", status: true }, { id : "a2", guid : "sdfsfd", ... value : "def", status: true }, ... ] Additionally I need to add this object to the array if an

debounce textarea input with react/redux

流过昼夜 提交于 2019-12-05 13:57:45
I'm trying to debounce textarea value with react/redux and show the debounced value in div#preview but i'm getting synthetic event warning after first function call. I have reducer for handling textarea value state which is working as intended, but for simplicity i've wrote local state in this snippet. If there is a better method besides debounce to avoid react rerendering after each keypress I would love to know. Thanks in advance. class TextArea extends React.Component { constructor(props){ super(props); this.state = {foo: ''} } handleInputChange = _.debounce((e) => { e.persist(); let value

Compare two arrays containing objects in order to calculate what changed?

大憨熊 提交于 2019-12-05 13:31:24
Using flat JavaScript or by making use of lodash, what is the simplest way (hoping lodash has a function) which I compare the following arrays and return the value which has changed: Before [ {id: 0, name: 'Bob', age: 27}, {id: 1, name: 'Frank', age: 32}, {id: 2, name: 'Joe', age: 38} ] After [ {id: 0, name: 'Bob', age: 27}, {id: 1, name: 'Frank', age: 33}, {id: 2, name: 'Joe', age: 38} ] So between before and after, Frank is now age 33, so how can I simply return: {id: 1, name: 'Frank', age: 33} Or a more desired result: {id: 1, age: 33} EDIT: As I got such a nice variation of answers to my

Javascript dotted object keys to object

穿精又带淫゛_ 提交于 2019-12-05 12:01:52
How do you convert a dotted keys into a javascript object and retain it's value? So I got this kind of response from an API and I need to parse it by key: value. { "property": "personal_info.address.city", "description": "Missing field" }, { "property": "personal_info.address.country", "description": "Missing field" }, So I achieved this: { 'personal_info.address.city': 'Missing field', 'personal_info.address.country': 'Missing field' } // by using this code (lodash) _.mapValues(_.keyBy(obj, 'property'), function(o) { return o.description; }) however, i need it to be like this: { personal_info

Import from node_modules not recognized in es6 modules in browser

廉价感情. 提交于 2019-12-05 11:20:28
I'm trying to use lodash in my web application. I have installed lodash using npm in my local project. I plan on using the ES6 modules in my code. Here is my main.js file: import * as _ from "lodash"; _.each([1, 2, 3, 4], (i) => { console.log('index each ' + i); }); And I have included it in index.html as: <script src="js/main.js", type="module"></script> But I get the following error in the browser console. Uncaught TypeError: Failed to resolve module specifier "lodash". Relative references must start with either "/", "./", or "../". Note: I do not wish to use any bundling tool. If you don't