Best lodash methods with Examples

March 22, 2023

Best lodash methods with Examples

Best  lodash methods with Examples

1) _.times()

Use lodash’s _.times() to create an array of, and use the generated index to create the id, and increment the date

import { times } from 'lodash';
import { add } from 'date-fns';

const startDate = new Date();

const time = times(2, (index) => ({
  id: index + 1,
  date: add(startDate, { days: index })
}));

console.log(time);
 
{id: 1, date: Wed Mar 22 2023 10:16:24 GMT+0530 (India Standard Time)}
{id: 2, date: Thu Mar 23 2023 10:16:24 GMT+0530 (India Standard Time)}

Written by Manoj Bhardwaj who lives and works in Dharamshala Himachal Pradesh (India). My stackoverflow