lodash

What is the difference between lodash's _.map and _.pluck?

拈花ヽ惹草 提交于 2019-11-30 18:30:49
I have the following code, can anyone tell the difference: let _ = require('lodash'); let arr = [ {'fname':'Ali', 'lname': 'Yousuf'}, {'fname': 'Uzair', 'lname': 'Ali'}, {'fname': 'Umair', 'lname': 'Khan'} ]; _.map(arr, 'fname'); _.pluck(arr, 'fname'); The output is the same, and both functions are not mutating arr . In the way you're using them, they basically do the same. That's why .pluck() was removed from Lodash v4.0.0 in favor of using .map() with a string as second argument. Here's the relevant excerpt from the changelog : Removed _.pluck in favor of _.map with iteratee shorthand var

lodash: check object is empty

非 Y 不嫁゛ 提交于 2019-11-30 17:13:31
I have this object: {"": undefined} and when I check this object for empty in this way: _.isEmpty({"": undefined}) I get false result, maybe in lodash we have another method? Your example object is not empty so instead perhaps you want to test if all properties are undefined let o = {foo: undefined}; !_.values(o).some(x => x !== undefined); // true Armen Zakaryan _.isEmpty(obj, true) var obj = { 'firstName': undefined , 'lastName' : undefined }; console.log(_.isEmpty(obj)); // false <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script> Please, see http://www

How to remove undefined values from array but keep 0 and null

折月煮酒 提交于 2019-11-30 16:47:42
In javascript, I want to remove undefined values, but keep the values 0 and null from an array. [ 1, 2, 3, undefined, 0, null ] How can I do it cleanly? Jaynam You can use _.compact(array); Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey. See: https://lodash.com/docs/4.15.0#compact The best way using lodash is _.without Example: const newArray = _.without([1,2,3,undefined,0,null], undefined); No need for libraries with modern browsers. filter is built in. var arr = [ 1, 2, 3, undefined, 0, null ]; var updated = arr.filter(function

Remap properties name and values using lodash

对着背影说爱祢 提交于 2019-11-30 14:46:52
I have this array: aItems = [{ "PropertyA": "apple", "PropertyB": "banana", "PropertyC": "dog", "PropertyD": "hotdog", "PropertyE": "coldcat", "PropertyF": "Y", "PropertyG": "N" }, ..., { "PropertyA": "this", "PropertyB": "is", "PropertyC": "json", "PropertyD": "code", "PropertyE": "wow", "PropertyF": "N", "PropertyG": "N" }] I would like use lodash to obtain this result: aItems = [{ "propertyA": "apple", "propertyB": "banana", "propertyC": "dog", "propertyD": "hotdog", "propertyE": "coldcat", "propertyNEW": true, "propertyG": false }, ..., { "propertyA": "this", "propertyB": "is", "propertyC"

Javascript: Convert dot-delimited strings to nested object value

那年仲夏 提交于 2019-11-30 13:58:42
I have a bunch of object attributes coming in as dot-delimited strings like "availability_meta.supplier.price" , and I need to assign a corresponding value to record['availability_meta']['supplier']['price'] and so on. Not everything is 3 levels deep: many are only 1 level deep and many are deeper than 3 levels. Is there a good way to assign this programmatically in Javascript? For example, I need: ["foo.bar.baz", 1] // --> record.foo.bar.baz = 1 ["qux.qaz", "abc"] // --> record.qux.qaz = "abc" ["foshizzle", 200] // --> record.foshizzle = 200 I imagine I could hack something together, but I

lodash: check object is empty

十年热恋 提交于 2019-11-30 13:41:18
问题 I have this object: {"": undefined} and when I check this object for empty in this way: _.isEmpty({"": undefined}) I get false result, maybe in lodash we have another method? 回答1: Your example object is not empty so instead perhaps you want to test if all properties are undefined let o = {foo: undefined}; !_.values(o).some(x => x !== undefined); // true 回答2: _.isEmpty(obj, true) var obj = { 'firstName': undefined , 'lastName' : undefined }; console.log(_.isEmpty(obj)); // false <script src=

Flatten array with objects into 1 object

十年热恋 提交于 2019-11-30 13:38:24
问题 Given input: [{ a: 1 }, { b: 2 }, { c: 3 }] How to return: { a: 1, b: 2, c: 3 } For arrays it's not a problem with lodash but here we have array of objects. 回答1: Use Object.assign: let merged = Object.assign(...arr); // ES6 (2015) syntax var merged = Object.assign.apply(Object, arr); // ES5 syntax Note that Object.assign is not yet implemented in many environment and you might need to polyfill it (either with core-js, another polyfill or using the polyfill on MDN). You mentioned lodash, so it

How to get an array of values based on an array of indexes?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 13:04:38
I have a set of two arrays. One contains some fruit values as strings, the other one contains some random numbers. Here I considered the number arrays are the indexes of the fruits array. How to get a new array of fruits given the numbers in the index array? Sample code: var resultArr = []; var fruitier = ["apple", "orange", "grapes", "pineapple", "fig", "banana", "jackfruit", "pomegranate"]; var indexArr = [0, 2, 4]; Output: resultArr = ["apple", "grapes", "fig"]; for(var i = 0; i < indexArr.length; i++) resultArr.push(fruitier[indexArr[i]]); Use .map : var resultArr = indexArr.map(i =>

Lodash union of arrays of objects

China☆狼群 提交于 2019-11-30 11:42:13
I'd like to use the _.union function to create a union of two arrays of objects. Union works with arrays of primitives only as it uses === to examine if two values are equal. I'd like to compare objects using a key property: objects with the same key property would be regarded equal. Is there a nice functional way to achieve that ideally using lodash? Craig Suchanec A non pure lodash way to do this but using the array.concat function you are able to do this pretty simply along uniq() : var objUnion = function(array1, array2, matcher) { var concated = array1.concat(array2) return _.uniq

Add new properties of object in lodash

爱⌒轻易说出口 提交于 2019-11-30 11:34:09
I've two objects and I want to add properties from object A to object B and I try with extend which doesn't work,do I need to use something different ? a = { name = "value" name2 = "value2" } b = { name3 = "value" name4 = "value2" } I want that A will contain both a = { name = "value" name2 = "value2" name3 = "value" name4 = "value2" } T.J. Crowder _.extend (now called _.assign ) is indeed how you do this: _.assign(a, b); Live Example : var a = { name: "value", name2: "value2" }; var b = { name3: "value", name4: "value2" }; _.assign(a, b); document.body.insertAdjacentHTML( "beforeend", "Result