基于lodash的实用函数

  • getCustomizedObject
/**
 * @number: the count of object attribute 
 * @prefix: the object attribute prefix string
 * @value: the value of each attribute, default is ''
 * @start: the default start is 1
 * Example: 
 * getCustomizedObject(3, 'amount') // {amount1:'', amount2:'', amount3:''}
 */
export const getCustomizedObject = (number, prefix, value='', start=1) => {
  const keys = _.times(number, (i)=> `${prefix}${i+start}`);
  const values = _.times(number, ()=> value);
  return _.zipObject(keys, values);
};
  • getCustomizedArray
/**
 * @number: the elment count of array
 * @prefix: the element value prefix string
 * @start: the default start is 1
 * Example: 
 * getCustomizedArray(3, 'amount', 2) // ['amount2', 'amount3', 'amount4']
 */
export const getCustomizedArray = (number, prefix, start=1) => {
  return _.times(number, (i) => `${prefix}${i+start}`);
};
posted @ 2023-02-17 10:54  箫笛  阅读(26)  评论(0)    收藏  举报