how to use the at function from lodash in javascript

The at function from Lodash allows you to get values of an array or object at certain indices or paths. Here is an example of how to use it with an array:

index.tsx
const _ = require('lodash');

const array = ['a', 'b', 'c', 'd', 'e'];
const indices = [1, 3, 4];

const values = _.at(array, indices);

console.log(values); // Output: ['b', 'd', 'e']
185 chars
9 lines

In this example, at takes two arguments: the array we want to manipulate (array) and the indices we want to retrieve the values from (indices). The function then returns a new array with the requested values.

You can also use at with objects to retrieve values at specific paths. Here is an example:

index.tsx
const _ = require('lodash');

const object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
const paths = ['a[0].b.c', 'a[1]'];

const values = _.at(object, paths);

console.log(values); // Output: [3, 4]
193 chars
9 lines

In this example, at takes the object we want to manipulate and an array of paths as arguments. The function returns an array with the requested values at the specified paths.

It's important to note that at returns an array, even if you only request the value at one index or path.

gistlibby LogSnag