How to use the toPairsIn function from lodash

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

lodash.toPairsIn creates an array of own and inherited enumerable string keyed-value pairs for an object.

391
392
393
394
395
396
397
398
399
400
module.exports.toInteger           = _.toInteger;
module.exports.toLength            = _.toLength;
module.exports.toLower             = _.toLower;
module.exports.toNumber            = _.toNumber;
module.exports.toPairs             = _.toPairs;
module.exports.toPairsIn           = _.toPairsIn;
module.exports.toPath              = _.toPath;
module.exports.toPlainObject       = _.toPlainObject;
module.exports.toQuery             = _.toQuery;
module.exports.toSafeInteger       = _.toSafeInteger;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

713
714
715
716
717
718
719
720
721
722
723
724
725
console.log(setWith); // => { 'a': [{ 'b': { 'c': 4 } }] }


const toPairs = _.toPairs({ 'a': 1, 'b': 2 });
console.log(toPairs); // => [['a', 1], ['b', 2]]


const toPairsIn = _.toPairsIn({ 'a': 1, 'b': 2 });
console.log(toPairsIn); // => [['a', 1], ['b', 2]]


const transform = _.transform({ 'a': 1, 'b': 2, 'c': 1 }, (result, value, key) => {
    (result[value] || (result[value] = [])).push(key);
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.toPairsIn work?

lodash.toPairsIn is a function that creates an array of key-value pairs for an object's own and inherited enumerable properties, similar to Object.entries, but also includes properties from the object's prototype chain.

11
12
13
14
15
16
17
18
19
20
21
22
23
}


Foo.prototype.c = 3;


// lodash
console.log("lod.toPairsIn(new Foo)", lod.toPairsIn(new Foo));
console.log("lod.entriesIn(new Foo)", lod.entriesIn(new Foo)); // alias


// es6
function toPairsIn(obj) {
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const _ = require("lodash");

function Foo() {
  this.a = 1;
  this.b = 2;
}

Foo.prototype.c = 3;

console.log(_.toPairsIn(new Foo()));
// Output: [['a', 1], ['b', 2], ['c', 3]]

In this example, lodash.toPairsIn is used to create an array of key-value pairs for all enumerable properties, including properties on the object's prototype, of the Foo object. The output shows an array of arrays where each subarray contains the property name as the first element and the property value as the second element.

Other functions in lodash

Sorted by popularity

function icon

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