How to use the initial function from lodash

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

lodash.initial is a function that returns all but the last element of an array.

5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
*  style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a slice of `array`.
* @example
*
* _.initial([1, 2, 3]);
* // => [1, 2]
*
* _.initial([1, 2, 3], 2);
* // => [1]
fork icon73
star icon711
watch icon29

+ 7 other calls in file

164
165
166
167
168
169
170
171
172
173
module.exports.implode             = _.implode;
module.exports.inRange             = _.inRange;
module.exports.inc                 = _.inc;
module.exports.includes            = _.includes;
module.exports.indexOf             = _.indexOf;
module.exports.initial             = _.initial;
module.exports.interpose           = _.interpose;
module.exports.intersection        = _.intersection;
module.exports.intersectionBy      = _.intersectionBy;
module.exports.intersectionWith    = _.intersectionWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.initial work?

lodash.initial is a function provided by the Lodash library that returns a new array with all the elements of the input array except for the last element. The function takes an array as its input and returns a new array without modifying the original array. It works by first checking if the input array is empty or not, and if it is empty, it simply returns an empty array. If the input array has one or more elements, it returns a new array with all the elements except the last one. The lodash.initial function can be useful in situations where you want to get a new array with all the elements of an existing array except for the last element. This can be useful in many different contexts, such as when you want to remove the last element from an array or when you want to perform some operation on all the elements of an array except the last one.

63
64
65
66
67
68
69
70
71
72
73
74
75
console.log(head); // => 1


const indexOf = _.indexOf([1, 2, 1, 2], 2);
console.log(indexOf); // => 1


const initial = _.initial([1, 2, 3]);
console.log(initial); // => [1, 2]


const intersection = _.intersection([2, 1], [2, 3]);
console.log(intersection); // => [2]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

81
82
83
84
85
86
87
88
89
90
 * @param {string} options.field - path of relation / attribute
 */
const normalizeFieldName = ({ model, field }) => {
  const fieldPath = field.split('.');
  return _.last(fieldPath) === 'id'
    ? _.initial(fieldPath)
        .concat(model.primaryKey)
        .join('.')
    : fieldPath.join('.');
};
fork icon0
star icon0
watch icon1

Ai Example

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

const myArray = [1, 2, 3, 4, 5];

const newArray = _.initial(myArray);

console.log(newArray); // Output: [1, 2, 3, 4]

In this example, we first require the Lodash library and create an array called myArray with five elements. We then use the _.initial function to create a new array called newArray with all the elements of myArray except for the last one. Finally, we log the contents of newArray to the console, which outputs [1, 2, 3, 4].

211
212
213
214
215
216
217
218
219
220
221
222
let indexOf2 = _.indexOf([1, 2, 1, 2], 2, 2);
console.log('indexOf2--->', indexOf2);
//indexOf2---> 3


//_.initial(array)
let initial1 = _.initial([1, 2, 3]);
console.log('initial1--->', initial1);
//initial1---> [ 1, 2 ]


//_.intersection([arrays])
fork icon0
star icon0
watch icon0

228
229
230
231
232
233
234
235
236
237
238
239
console.log('rearged1--->', rearged1);
//rearged1---> [ 'a', 'b', 'c' ]


//_.rest(func,[start=func.length-1])
var say = _.rest(function (what, names) {
    return what + ' ' + _.initial(names).join(', ') +
        (_.size(names) > 1 ? ', & ' : '') + _.last(names);
});


var say1 = say('hello', 'fred', 'barney', 'pebbles');
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Other functions in lodash

Sorted by popularity

function icon

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