How to use the compact function from underscore
Find comprehensive JavaScript underscore.compact code examples handpicked from public code repositorys.
underscore.compact is a function in the Underscore.js library that creates a new array with all falsey values removed.
GitHub: gautam-16/prod
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
let fieldIds = _.compact(_.uniq(_.pluck(inventoryData,'fieldId'))) fieldIds.forEach(fieldId => { let fieldData = _.reject(inventoryData,(fieldObj)=>{ return fieldObj.fieldId != fieldId }) let values = _.compact(_.uniq(_.pluck(fieldData,'value'))); let inventoryObject = {}; if(fieldData && fieldData.length > 0) { inventoryObject.fieldId = fieldData[0].fieldId;
418 419 420 421 422 423 424 425 426 427
filters: [ ['_id', 'in', orgIds] ], fields: ['parents'] }); organizations_parents = _.compact(_.uniq(_.flatten([orgIds, _.pluck(orgs, 'parents')]))); return !!(await suObj.directUpdate(suId, { organizations_parents: organizations_parents })) },
How does underscore.compact work?
underscore.compact is a function in the Underscore.js library that creates a new array with all falsey values removed. When you call underscore.compact(array), the function creates a new array with all the elements of the input array that are not falsey values. Falsey values include false, null, undefined, 0, NaN, and an empty string (""). The function iterates over each element in the array and checks if it is a falsey value. If the element is not falsey, it is added to the new array. The function returns the new array with all falsey values removed, while leaving the original array unchanged. In essence, underscore.compact provides a way to remove all falsey values from an array, creating a new array with only the truthy values.
94 95 96 97 98 99 100 101 102 103
selected_value = _.map(field.options, function(option) { if (_.contains(value, option.value)) { return option.label; } }); return _.compact(selected_value).join(','); case 'checkbox': if (_.isString(value)) { value = [value]; }
64 65 66 67 68 69 70 71 72
} }); }); // Add the new entries to the end of the cache. entries = _.compact(entries); for (var i = 0; i < entries.length; i++) { this._entries.push(entries[i]); }
Ai Example
1 2 3 4 5 6 7 8
const _ = require("underscore"); const array = [0, 1, false, 2, "", 3, null, undefined, NaN]; const newArray = _.compact(array); console.log(newArray); // Output: [1, 2, 3]
In this example, we first import the underscore library. We then define an array with several falsey values, including 0, false, an empty string (""), null, undefined, and NaN. We then call _.compact(array) to create a new array with all falsey values removed. The function returns a new array containing only the truthy values [1, 2, 3]. We then log the new array to the console. Note that in this example, we're using an array with several falsey values for the sake of example, but in a real application you would typically use underscore.compact to remove falsey values from an array of data.
underscore.keys is the most popular function in underscore (11266 examples)