怎么替換數組中對象的屬性


用es6
第一種: ...item根據你的需求,可要可不要

    let arr = [
      {id: 1, title: "綁定手機"},
      {id: 2, title: "實名認證"},
      {id: 3, title: "游戲分享任務"},
      {id: 12, title: "游戲體驗任務"},
      {id: 13, title: "游戲時長任務"}
    ]
    const res = arr.map(item=>({...item, label: item.title, value: item.id}))
    console.log(res);

第二種: 比較傻瓜的

  let arr = [
      {id: 1, title: "綁定手機"},
      {id: 2, title: "實名認證"},
      {id: 3, title: "游戲分享任務"},
      {id: 12, title: "游戲體驗任務"},
      {id: 13, title: "游戲時長任務"}
    ]
    let arrNew = arr.map((item) => {
      item.label = item.title
      item.value = item.id
      return item
    })
    console.log(arrNew);

好好生活-_-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM