How to use the chain function from lodash

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

7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
*   { 'name': 'barney',  'age': 36 },
*   { 'name': 'fred',    'age': 40 },
*   { 'name': 'pebbles', 'age': 1 }
* ];
*
* var youngest = _.chain(characters)
*     .sortBy('age')
*     .map(function(chr) { return chr.name + ' is ' + chr.age; })
*     .first()
*     .value();
fork icon73
star icon711
watch icon29

46
47
48
49
50
51
52
53
54
55
module.exports.camelCase           = _.camelCase;
module.exports.capitalize          = _.capitalize;
module.exports.castArray           = _.castArray;
module.exports.cat                 = _.cat;
module.exports.ceil                = _.ceil;
module.exports.chain               = _.chain;
module.exports.chunk               = _.chunk;
module.exports.chunkAll            = _.chunkAll;
module.exports.chunkContrib        = _.chunkContrib;
module.exports.clamp               = _.clamp;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

137
138
139
140
141
142
143
144
145
146
    settings: {},
    others: {},
    sticker: {},
    ...(global.db.data || {})
  }
  global.db.chain = _.chain(global.db.data)
}
loadDatabase()
global.db = JSON.parse(fs.readFileSync('./src/database.json'))
if (global.db) global.db = {
fork icon629
star icon36
watch icon2

92
93
94
95
96
97
98
99
100
101
102
  const sources = [
    tagValue(exif, 'IPTC', 'Keywords'),
    tagValue(exif, 'XMP', 'Subject'),
    picasaValue(picasa, 'keywords')
  ]
  return _.chain(sources).flatMap(makeArray).uniq().value()
}


function people (exif) {
  return tagValue(exif, 'XMP', 'PersonInImage') || []
fork icon80
star icon649
watch icon21

+ 4 other calls in file

136
137
138
139
140
141
142
143
144
145
});

const createFunctionStatement = getFunctionsScript(schemaName, udfs);
const createProceduresStatement = getProceduresScript(schemaName, procedures);

return _.chain([schemaStatement, createFunctionStatement, createProceduresStatement])
	.compact()
	.map(_.trim)
	.join('\n\n')
	.trim()
fork icon8
star icon2
watch icon0

94
95
96
97
98
99
100
101
102
103
104
    global.chatgpt.READ = null;
    global.chatgpt.data = {
        users: {},
        ...(global.chatgpt.data || {})
    };
    global.chatgpt.chain = lodash.chain(global.chatgpt.data);
};


loadChatgptDB();   

fork icon6
star icon6
watch icon2

76
77
78
79
80
81
82
83
84
85
return {
  numero,
  suffixe,
  lieuDitComplementNom,
  parcelles: parcelles || [],
  sources: chain(numeroAdresses).map('source').uniq().value(),
  certifie,
  position,
  positionType,
  positions: position ? [{position, positionType}] : [],
fork icon3
star icon7
watch icon3

+ 3 other calls in file

150
151
152
153
154
155
156
157
158
159
  .map(v => ({...v, numeros: numerosGeolocalises[v.idVoie]}))

if (voiesAvecNumeros.length > 0) {
  adressesStream.write(buildMunicipality(
    commune,
    chain(voiesAvecNumeros).map('numeros').flatten().filter(n => n.position).value()
  ))
  communesWithAdresses.push(codeCommune)

  const duplicateSlugs = chain(voiesAvecNumeros)
fork icon3
star icon7
watch icon3

+ 3 other calls in file

86
87
88
89
90
91
92
93
94
95
const voies = composedVoies.map(v => ({
  type: 'voie',
  ...omit(v, 'numeros')
}))

const numeros = chain(composedVoies)
  .map(v => v.numeros.map(n => ({
    codeCommune,
    idVoie: v.idVoie,
    ...n
fork icon3
star icon7
watch icon3

739
740
741
742
743
744
745
746
747
748
749
750
751
console.log(valuesIn); // => [1, 2, 3]




// Seq


const chain = _.chain([1, 2, 3, 4])
    .filter(n => n % 2 == 0)
    .map(n => n * n)
    .value();
console.log(chain); // => [4, 16]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

70
71
72
73
74
75
76
77
78
79
	}

	return output
}, {})

topLevelDeclarations = _.chain(root.body)
	.map(node => {
		if (node.type === 'ExportDefaultDeclaration' || node.type === 'ExportNamedDeclaration') {
			return node.declaration
		}
fork icon2
star icon3
watch icon1

+ 4 other calls in file

492
493
494
495
496
497
498
499
500
501
502
  const exposedFieldsWithCounters = Array.from(exposedMap, ([name, value]) => ({ name, value }));
  exposedMap.clear();
  return exposedFieldsWithCounters;
};


const groupOptions = (options, obj) => _.chain(options)
  .map((option) => ({
    ...option,
    body: jsonHelper.parseJson(option.body),
    ...(obj && { author_permlink: obj.author_permlink, price: obj.price, avatar: obj.avatar }),
fork icon0
star icon3
watch icon3

+ 5 other calls in file

278
279
280
281
282
283
284
285
286
if (sat_org_id) {
    const batchDetailInfo = await inventory.getSystemDetailsBatch(all_systems);
    _.forEach(normalizedIssues, issue => {
        issue.systems = _.filter(issue.systems, system => {
            // eslint-disable-next-line security/detect-object-injection
            const org_id = _.chain(batchDetailInfo[system].facts)
            .find(SATELLITE_NAMESPACE)
            .get('facts.organization_id')
            .toString(); //organization_id is an int (boo!)
fork icon17
star icon1
watch icon8

+ 4 other calls in file

69
70
71
72
73
74
75
76
77
78
chainWebpack: config => {
  /**
   * 添加 CDN 参数到 htmlWebpackPlugin 配置中
   * 已适配多页
   */
  const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
  const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
  each(targetHtmlPluginNames, name => {
    config.plugin(name).tap(options => {
      set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
fork icon5
star icon14
watch icon2

76
77
78
79
80
81
82
83
84
85

openaps.analyzeData = function analyzeData (sbx) {
  var recentHours = 6; //TODO dia*2
  var recentMills = sbx.time - times.hours(recentHours).msecs;

  var recentData = _.chain(sbx.data.devicestatus)
    .filter(function(status) {
      return ('openaps' in status) && sbx.entryMills(status) <= sbx.time && sbx.entryMills(status) >= recentMills;
    })
    .map(function(status) {
fork icon0
star icon1
watch icon1

+ 3 other calls in file

17
18
19
20
21
22
23
24
25
26

var allRenderOverOptions = [5, 1, 0.5, 0.1];
if (_.isNumber(bolusSettings.renderOver) && bolusSettings.renderOver > 0 && bolusSettings.renderOver < Number.MAX_SAFE_INTEGER) {
  allRenderOverOptions.push(_.toNumber(bolusSettings.renderOver));
}
var sortedRenderOverOptions = _.chain(allRenderOverOptions).uniq().sort().reverse().value();

_.forEach(sortedRenderOverOptions, function (optionValue) {
  $('#bolusRenderOver').append(
    $('<option></option>')
fork icon0
star icon1
watch icon1

+ 2 other calls in file

50
51
52
53
54
55
56
57
58
59
  });
}

iob.isDeviceStatusAvailable = function isDeviceStatusAvailable (devicestatus) {

  return _.chain(devicestatus)
      .map(iob.fromDeviceStatus)
      .reject(_.isEmpty)
      .value()
      .length > 0;
fork icon0
star icon1
watch icon1

+ 5 other calls in file

62
63
64
65
66
67
68
69
70
71
  })
}

var getExitCode = main.getExitCode = function (mdResults) {
  var evaluations = _.chain(mdResults).map('evaluated').flatten().value()
  var evalResults = _.chain(evaluations).map('evalResult').flatten().value()
  var evalResultsInstanceofError = _.map(evalResults, function (evalResult) {
    return evalResult instanceof Error
  })
  var evalResultsHasInstanceofError = _.contains(evalResultsInstanceofError, true)
fork icon0
star icon1
watch icon2

+ 7 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
}




module.exports.filterBooks = function (term) {
	
	return _.chain(books)
	.flatMap(booksCollections => booksCollections)
	.filter(book => 
				_c.strContains(
					_c.lowerCase(book["BOOK NAME"]), 
fork icon2
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

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