Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.prototype.commit() method is used to implement the chain sequence type and find the wrapped output.
Syntax:
_.prototype.commit()
Parameters: This method does not accept any parameter.
Return Value: This method returns the new lodash wrapper instance.
Example 1:
Javascript
// Requiring lodash library const _ = require('lodash'); Â Â // Creating an array of integers var arr = [5, 6]; Â Â // Pushing an integer into the array var wrapper = _(arr).push(7); Â Â Â // Using the commit() method wrapper = wrapper.commit(); Â Â // Displays output console.log(arr); |
Output:
[ 5, 6, 7 ]
Example 2:
Javascript
// Requiring lodash library const _ = require('lodash'); Â Â // Creating an array of strings var arr = ['Geeks', 'for']; Â Â // Pushing a string into the array var wrapper = _(arr).push('Geeks'); Â Â Â // Using the commit() method wrapper = wrapper.commit(); Â Â // Displays output console.log(arr); |
Output:
[ 'Geeks', 'for', 'Geeks' ]
Example 3: In this example, the wrapper object is returned.
Javascript
// Requiring lodash library const _ = require('lodash'); Â Â // Using the commit() method obj = _(['G', 'f']).reverse().commit(); Â Â // Displays output console.log(obj); |
Output:
LodashWrapper {
__wrapped__: [ 'f', 'G' ],
__actions__: [],
__chain__: false,
__index__: 0,
__values__: undefined
}
