极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。

# 解构赋值一个数组,a 取第一项默认值为 3,c 取剩下的值组成数组

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 540 (opens new window)

const list = [1, 2, 3, 4, 5];
const [a, ...c] = list;
function getTargetAndRest(target, originList) {
  let targetArr = [];
  for (let i = 0; i < originList.length; i++) {
    if (originList[i] === target) {
      targetArr = originList.splice(i, 1);
      break;
    }
  }
  return targetArr.concat(originList);
}

const list = [1, 2, 3, 4, 5];
let [a, ...c] = getTargetAndRest(3, list);
Last Updated: 2/23/2022, 11:56:07 AM