JAVASCRIPT
VUE
REACT
NODE
ES6
TYPESCRIPT

同类型列表下编号

2023. 02. 23    

同类型列表下编号

给同类型的item列表编号

let arr = [
  { type: 1, name: 'sdssd' },
  { type: 1, name: 'sdssd' },
  { type: 1, name: 'sdssd' },
  { type: 1, name: 'sdssd' },
  { type: 1, name: 'sdssd' },
  { type: 2, name: '就撒开了的' },
  { type: 2, name: '就撒开了的' },
  { type: 3, name: '无钱无权' },
  { type: 3, name: '无钱无权' }];
arr.map((item, i) => {
  console.log(item.name+' '+ ss(arr, item.type, i))
})
function ss(arr, type, listIndex) {
  const textIndex = arr.findIndex(function (val) {
    return val.type === type
  })
  let index = listIndex - (textIndex - 1)
  return index;
}