How to use the reverse function from lodash

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

lodash.reverse is a function that reverses the order of elements in an array.

326
327
328
329
330
331
332
333
334
335
module.exports.repeat              = _.repeat;
module.exports.repeatContrib       = _.repeatContrib;
module.exports.replace             = _.replace;
module.exports.rest                = _.rest;
module.exports.result              = _.result;
module.exports.reverse             = _.reverse;
module.exports.reverseOrder        = _.reverseOrder;
module.exports.round               = _.round;
module.exports.runInContext        = _.runInContext;
module.exports.sample              = _.sample;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

126
127
128
129
130
131
132
133
134
135
/**
 * 计算版本号大小
 */
exports.genWeight = function (version, buildNum) {
  let tmp = version.split('.');
  tmp = _.reverse(tmp);
  let weight = 0;
  tmp.forEach(function (t, i) {
    weight += Number(t) * Math.pow(10000, i);
  });
fork icon12
star icon36
watch icon7

How does lodash.reverse work?

lodash.reverse is a function that takes an array as an argument and returns a new array with the same elements in reverse order.

For example, if the input array is [1, 2, 3], the output array will be [3, 2, 1].

255
256
257
258
259
260
261
262
263
264
var status = code.match(/^2.*$/) ? 'success' : 'error';
response[status] = true;
response.id = operationId + '_' + code;

if (response.hasOwnProperty('x-examples-per-version')) {
    const sortedExamples = _.reverse(_.sortBy(response['x-examples-per-version'], ['x-version']));
    const closestExample = _.find(sortedExamples, function(exemplePerVersion) {
        return exemplePerVersion['x-version'] <= version;
    });
    if (closestExample === undefined) {
fork icon64
star icon31
watch icon34

+ 7 other calls in file

48
49
50
51
52
53
54
55
56
57
function borders(grid) {
  return [
    grid[0],
    _.zip(...grid)[grid.length-1],
    _.reverse([...grid[grid.length-1]]),
    _.reverse([..._.zip(...grid)[0]])
  ]
}

function rotateRight(grid) {
fork icon1
star icon4
watch icon3

+ 5 other calls in file

Ai Example

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

const arr = [1, 2, 3, 4, 5];
const reversedArr = _.reverse(arr);

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

In this example, lodash.reverse is used to reverse the order of elements in an array. The original array [1, 2, 3, 4, 5] is passed to the function and a new array [5, 4, 3, 2, 1] is returned with the elements in reversed order.

102
103
104
105
106
107
108
109
110
111

devGlobs.forEach(function (devGlob) {
    var devFiles = glob.sync(devGlob, {
        ignore: ignoreGlobs
    });
    devFiles = _.reverse(devFiles); // assure the elements are in right order

    devFiles.forEach(function (devFile) {
        paths.push(devFile);
    });
fork icon3
star icon2
watch icon0

105
106
107
108
109
110
111
112
113
114
115
116
117
console.log(pullAt); // => ['b', 'd']


const remove = _.remove([1, 2, 3, 4], n => n % 2 === 0);
console.log(remove); // => [2, 4]


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


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

+ 15 other calls in file

141
142
143
144
145
146
147
148
149
150
    principles.push({ label: `p${i}`, value: null, hidden: true });
  }
  return principles;
};
// levelsDisplay() {
//   return _.reverse(
//     _.uniqWith(
//       this.levels.map((l) => {
//         return { name: l.attributes.name, code: l.attributes.code };
//       }),
fork icon0
star icon0
watch icon2

+ 119 other calls in file

100
101
102
103
104
105
106
107
108
  //console.info('>>>sortField', field);
  if (field) {
    results = _.sortBy(results, field);
    if (-1 === wrapper.sortQuery[field]) {
      //console.info('>>>sort reverse');
      results = _.reverse(results);
    }
  }
}
fork icon0
star icon0
watch icon0

+ 2 other calls in file

302
303
304
305
306
307
308
309
310
311
312
313
console.log('remove1--->', remove1);
//removeArr---> [ 1, 3 ]
//remove1---> [ 2, 4 ]


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


//_.slice(array,[start=0],[end=array.length])
fork icon0
star icon0
watch icon0

21
22
23
24
25
26
27
28
29
30
31
32
  _.map(users, value => value.age)
  .reduce((previousValue, currentValue) => previousValue + currentValue)
);


console.log(
  _.reverse(users)
);


console.log(
  _.sortedUniqBy(users, value => value.age)
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

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