How to use the pickAll function from ramda

Find comprehensive JavaScript ramda.pickAll code examples handpicked from public code repositorys.

ramda.pickAll is a function that returns a new object with only the specified properties from a source object, including undefined values for properties not present in the source object.

7
8
9
10
11
12
13
14
15
16
17
18
19
console.log('pick', pick(obj));


const pick2 = R.pick(['a', 'e', 'f']); //=> {a: 1}
console.log('pick2', pick2(obj));  // if not present, it returns nothing


const pickAll = R.pickAll(['a', 'e', 'f']); // => {a: 1, e: undefined, f: undefined}
console.log('pickBy', pickAll(obj));


// pickBy is similar to filer the object 
const isEven = (val, key) => val % 2 === 0;
fork icon0
star icon1
watch icon1

+ 131 other calls in file

3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
 * @return {Object} A new object with only properties from `names` on it.
 * @see R.pick
 * @example
 *
 *      R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
 *      R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
 */
var pickAll = _curry2(function pickAll(names, obj) {
    var result = {};
    var idx = 0;
fork icon0
star icon0
watch icon1

+ 37 other calls in file

How does ramda.pickAll work?

ramda.pickAll is a function that takes a list of property names as its first argument and an object as its second argument. The function returns a new object that contains only the specified properties from the source object, including undefined values for properties not present in the source object.

Here's how ramda.pickAll works in detail:

  1. The function first checks if the second argument is an object. If it's not, it immediately returns an empty object.
  2. Next, it creates a new empty object that will be used to store the picked properties.
  3. Then, it iterates over each property name in the list provided as the first argument.
  4. For each property name, it checks if the property exists on the source object. If it does, it adds it to the new object with the same property name and value.
  5. If the property does not exist on the source object, it adds it to the new object with an undefined value.
  6. Finally, the function returns the new object containing only the specified properties.

Here's an example of how you could use ramda.pickAll to pick certain properties from an object:

javascript
const R = require('ramda'); const sourceObj = { name: 'John', age: 30, occupation: 'Engineer' }; const pickedObj = R.pickAll(['name', 'occupation', 'salary'], sourceObj); console.log(pickedObj); // Output: { name: 'John', occupation: 'Engineer', salary: undefined }

In this example, we're requiring the ramda package and importing the pickAll function from it.

We're then defining an object called sourceObj that has three properties: name, age, and occupation.

We're then calling R.pickAll with an array of property names to pick: ['name', 'occupation', 'salary'] and sourceObj as arguments. Since the salary property is not present in sourceObj, the new object returned by R.pickAll has an undefined value for the salary property.

11251
11252
11253
11254
11255
11256
11257
11258
11259
* @param {Object} obj The object to copy from
* @return {Object} A new object with only properties from `names` on it.
* @see R.pick
* @example
*
*      R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
*      R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
*/

fork icon0
star icon0
watch icon2

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
const R = require("ramda");

const sourceObj = { name: "John", age: 30, occupation: "Engineer" };
const pickedObj = R.pickAll(["name", "occupation", "salary"], sourceObj);

console.log(pickedObj); // Output: { name: 'John', occupation: 'Engineer', salary: undefined }

In this example, we're requiring the ramda package and importing the pickAll function from it. We're then defining an object called sourceObj that has three properties: name, age, and occupation. We're then calling R.pickAll with an array of property names to pick: ['name', 'occupation', 'salary'] and sourceObj as arguments. Since the salary property is not present in sourceObj, the new object returned by R.pickAll has an undefined value for the salary property. The output of console.log(pickedObj) is { name: 'John', occupation: 'Engineer', salary: undefined }.

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)