TeachingNote

js数组对象,数组集合。js集合数组找位置找下标。查找数组中某个对象的位置。数组根据下标删除

电脑版发表于:2023/11/21 9:42

手写 for 循环

  1. let arr = [{
  2. name: 'jin',
  3. code: 1
  4. }, {
  5. name: 'chang',
  6. code: 2
  7. } ...]
  8. let obj = {
  9. name: 'jin',
  10. code: 1
  11. }
  12. for(let i = 0, iLen = arr.length; i < iLen; i++) {
  13. if (arr[i].code === obj.code) {
  14. return i
  15. }
  16. }

使用findIndex

  1. let arr = [{
  2. name: 'jin',
  3. code: 1
  4. }, {
  5. name: 'chang',
  6. code: 2
  7. } ...]
  8. let obj = {
  9. name: 'jin',
  10. code: 1
  11. }
  12. arr.findIndex((el) => {
  13. return el.code === obj.code
  14. })
ts中的使用
  1. let index = _circlesItem?.taskLikes.findIndex((el:any) => el.likeDescId == res.data.taskLike.likeDescId && el.userId == res.data.taskLike.userId)

数组根据下标删除

  1. _circlesItem?.taskLikes.splice(index, 1);
关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
猜你喜欢
      /