亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更新對象的嵌套數組內的屬性

更新對象的嵌套數組內的屬性

天涯盡頭無女友 2023-09-21 17:18:53
我有這個數組,我想使用我嘗試過的 id 更新特定對象內選定的frameId模板,但是如果我選擇frameId = 1,則所有frameId 1模板都會被編輯,我只想要選定的id 。 sizes.map(size => {      if(size.id === id){        size.frames.map(frame => {          if(frame.frameId === frameId){            frame.template = {test: '101'}          }          return frame        })      }      return size    })[    {      id: 1,      name: 'Mrec',      frames: [        {          frameId: 1,          template: {},          img: [],        },        {          frameId: 2,          template: {},          img: [],        },      ],      active: false    },    {      id: 2,      name: 'Leaderboard',      frames: [        {          frameId: 1,          template: {},          img: [],        },        {          frameId: 2,          template: {},          img: [],        },      ],      active: false    }]
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

我認為正確的方法是這樣的。(#edit:它適用于片段)


var id = 1,

frameId=2;

var sizes = [

    {

      id: 1,

      name: 'Mrec',

      frames: [

        {

          frameId: 1,

          template: {},

          img: [],

        },

        {

          frameId: 2,

          template: {},

          img: [],

        },

      ],

      active: false

    },

    {

      id: 2,

      name: 'Leaderboard',

      frames: [

        {

          frameId: 1,

          template: {},

          img: [],

        },

        {

          frameId: 2,

          template: {},

          img: [],

        },

      ],

      active: false

    }

]


sizes.map(size => {

        let frames;

        if (size.id === id) {

            frames = size.frames.map(frame => {

                if (frame.frameId === frameId) {

                    frame.template = { test: '101' }

                }

                return frame;

            })

        } else {

            frames = size.frames;

        }

        return {...size,frames}

    })

document.write(JSON.stringify(sizes))


查看完整回答
反對 回復 2023-09-21
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號