其他分享
首页 > 其他分享> > 改变一个数组内元素的位置,不通过splice方法。

改变一个数组内元素的位置,不通过splice方法。

作者:互联网

这个数据

现在已经完成了,将本来在第一位的18代金券改到第31位,下面说一下怎么实现的。

//currHotRightsTypeSorted这个是数据源头,legalRightsType这个是数据的分类,因为18元代金券只在两个分类中
//1对数据进行遍历,找打需要分类中某个元素并赋值给一个新的元素,以后用,并删除他,通过他的i值。
//2如果数据分类数据中的长度小于31位时,放到最后一位。
  currHotRightsTypeSorted.forEach((num: any) => {
            let tmp = '';
            if (num.legalRightsType == '1015' || num.legalRightsType == '----') {
              for (let i = 0; i < num.list.length; i++) {
                if (num.list[i].legalRightsNbr === 'QY1622613823700') {
                  tmp = num.list.splice(i, 1);
                }
              }
              if (num.list.length >= 31) {
                num.list.splice(30, 0, tmp[0]);
              } else {
                num.list.push(tmp[0]);
              }
            }
          });

标签:tmp,splice,31,元素,list,num,数组,legalRightsType
来源: https://www.cnblogs.com/xuelinomen/p/16175520.html