How to use the at function from lodash

Find comprehensive JavaScript lodash.at code examples handpicked from public code repositorys.

lodash.at is a method in the Lodash library that retrieves a value from an object based on a given path.

3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
 * @example
 *
 * _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
 * // => ['a', 'c', 'e']
 *
 * _.at(['fred', 'barney', 'pebbles'], 0, 2);
 * // => ['fred', 'pebbles']
 */
function at(collection) {
  var args = arguments,
fork icon73
star icon711
watch icon29

+ 3 other calls in file

24
25
26
27
28
29
30
31
32
33
module.exports.ary                 = _.ary;
module.exports.assign              = _.assign;
module.exports.assignIn            = _.assignIn;
module.exports.assignInWith        = _.assignInWith;
module.exports.assignWith          = _.assignWith;
module.exports.at                  = _.at;
module.exports.attempt             = _.attempt;
module.exports.before              = _.before;
module.exports.best                = _.best;
module.exports.binPick             = _.binPick;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.at work?

lodash.at is a function that retrieves the values at specified paths of an object and returns an array of the corresponding values. It accepts two arguments, the first is the object to be searched, and the second is an array of strings or stringified paths where the values should be retrieved. The function uses lodash.toPath to convert the string paths to an array, then iterates through each path and uses lodash.get to retrieve the corresponding value. If the path does not exist in the object, undefined is returned.

605
606
607
608
609
610
611
612
613
614
615
616
617
console.log(assignInWith); // => { 'a': 1, 'b': 2, 'c': 3 }


const assignWith = _.assignWith({ 'a': 1 }, { 'b': 2 }, { 'c': 3 }, (a, b) => a + b);
console.log(assignWith); // => { 'a': 1, 'b': 2, 'c': 3 }


const at = _.at({ 'a': [{ 'b': { 'c': 3 } }, 4] }, ['a[0].b.c', 'a[1]']);
console.log(at); // => [3, 4]


const create = _.create({ 'a': 1 }, { 'b': 2 });
console.log(create); // => { 'a': 1, 'b': 2 }
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const _ = require("lodash");

const obj = {
  user: {
    name: "John",
    age: 30,
    posts: [
      { title: "First Post", content: "Hello World!" },
      { title: "Second Post", content: "Hello Universe!" },
    ],
  },
};

const name = _.at(obj, "user.name");
console.log(name); // Output: ['John']

const firstPostContent = _.at(obj, "user.posts[0].content");
console.log(firstPostContent); // Output: ['Hello World!']

const secondPostTitle = _.at(obj, ["user", "posts", 1, "title"]);
console.log(secondPostTitle); // Output: ['Second Post']

In this example, lodash.at is used to extract values from a nested object. The first argument is the object from which we want to extract values, and the second argument is the path to the value that we want to extract. We can pass the path as a string or as an array of strings. If the path contains an array index, we can use the square bracket notation to access it. The function returns an array of values, even if we are only extracting one value.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)