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

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

為對象中的多個數組分配相同的值

為對象中的多個數組分配相同的值

米琪卡哇伊 2023-03-24 14:24:18
您好我正在嘗試將單個值分配給單個對象中的多個動態數組和嵌套數組。這是這里的對象:object = {  metaForm: [{      id: 1,      text: 'abc',      AdditionalVal: []    },    {      id: 1,      text: 'abc',      AdditionalVal: [{        id: 1,        text: 'add',        compositeConfig: []      }, ]    },    {      id: 1,      text: 'abc',      AdditionalVal: [{        id: 1,        text: '123',        compositeConfig: [{          id: 1,          text: 'composit',          compositeConfig: []        }]      }, ]    }  ],  tabData: [{      composite: false,      compositeFieldList: [],      id: 3576,      tenantId: "1",    },    {      composite: false,      compositeFieldList: [{          id: 1,          text: 'composit2',          compositeConfig: []        },        {          id: 1,          text: 'composit3',          compositeConfig: []        },      ],      id: 3576,      tenantId: "1",    },  ]}下面是 o/pobject = {  metaForm: [{      id: 1,      text: 'abc',      AdditionalVal: [],      isDisabled: true,    },    {      id: 1,      text: 'abc',      isDisabled: true,      AdditionalVal: [{        id: 1,        text: 'add',        isDisabled: true,        compositeConfig: []      }, ]    },    {      id: 1,      text: 'abc',      isDisabled: true,      AdditionalVal: [{        id: 1,        text: '123',        isDisabled: true,        compositeConfig: [{          id: 1,          text: 'composit',          isDisabled: true,          compositeConfig: []        }]      }, ]    }  ],  tabData: [{      composite: false,      compositeFieldList: [],      id: 3576,      isDisabled: true,      tenantId: "1",    },    {      composite: false,      isDisabled: true,      compositeFieldList: [{          id: 1,          text: 'composit2',          isDisabled: true,          compositeConfig: []        }      在 Above 對象中,分配了一個新值“isDisabled: true”,該值分配給所有數組以及內部或嵌套數組。如何為多個動態數組賦值,無論有多少數組或嵌套數組
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

編輯:錯過了問題中的“嵌套數組”部分。


試試這個:


function setProperty(object, key, value) {

  if(Array.isArray(object)){

    let newArray = [];

    object.forEach((obj, index) => {

      newArray.push(setProperty(obj, key, value));

    });

    return newArray;

  } else if(typeof object === 'object' && object != null) {

    object[key] = value;

    Object.entries(object).forEach(item => {

      if(Array.isArray(item[1])) {

        let listKey = item[0];

        let newList = setProperty(item[1], key, value);

        object[listKey] = newList;

      }

    });

    return object;

  }

}


用法


setProperty(object, "isDisabled", true);

setProperty(object, "isDisabled", false);

setProperty(object, "someKey", "Some Value");

運行以下代碼片段進行演示


function setProperty(object, key, value) {

  if (Array.isArray(object)) {

    let newArray = [];

    object.forEach((obj, index) => {

      newArray.push(setProperty(obj, key, value));

    });

    return newArray;

  } else if (typeof object === 'object' && object != null) {

    object[key] = value;

    Object.entries(object).forEach(item => {

      if (Array.isArray(item[1])) {

        let listKey = item[0];

        let newList = setProperty(item[1], key, value);

        object[listKey] = newList;

      }

    });

    return object;

  }

}


function changeProp() {

  let object = {

    metaForm: [{

        id: 1,

        text: 'abc',

        AdditionalVal: []

      },

      {

        id: 1,

        text: 'abc',

        AdditionalVal: [{

          id: 1,

          text: 'add',

          compositeConfig: []

        }, ]

      },

      {

        id: 1,

        text: 'abc',

        AdditionalVal: [{

          id: 1,

          text: '123',

          compositeConfig: [{


            id: 1,

            text: 'composit',

            compositeConfig: []


          }]

        }, ]

      }

    ],

    tabData: [{

        composite: false,

        compositeFieldList: [],

        id: 3576,

        tenantId: "1",

      },

      {

        composite: false,

        compositeFieldList: [{


            id: 1,

            text: 'composit2',

            compositeConfig: []


          },

          {


            id: 1,

            text: 'composit3',

            compositeConfig: []


          },

        ],

        id: 3576,

        tenantId: "1",

      },


    ]

  };

  document.getElementById("old").innerHTML = JSON.stringify(object, null, 4);

  let name = document.getElementById("propertyName").value;

  if (name == null || name == "")

    name = "isDisabled"

  setProperty(object, name, true);

  document.getElementById("result").innerHTML = JSON.stringify(object, null, 4);

}

<div>

  <h3> Enter the name of the property and click on the <strong>Set Property</strong> button.<br/> Default name is <i>isDisabled</i></h3>

  <input type="text" id="propertyName" name="propertyName" placeholder="Property name" />

  <button onclick="changeProp();">Set Property</button>

  <h2>Object</h2>

  <pre id="old">

</pre>

  <br/>

  <h2>Updated</h2>

  <pre id="result">

</pre>

</div>


查看完整回答
反對 回復 2023-03-24
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

object.metaForm.forEach(i => {

 i.isDisabled = true;

 if(i.AdditionalVal.length){

  i.AdditionalVal.forEach(v => {

    if.isDisabled = true;

  })

 }

});

object.tabData.forEach(i => {

 i.isDisabled = true;

 if(i.compositeFieldList.length){

  i.compositeFieldList.forEach(c => {

   c.isDisabled = true;

  }

 }

})


查看完整回答
反對 回復 2023-03-24
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

那里沒有神奇的方法。

你應該只寫簡單的遞歸。

這樣的事情應該有效(偽代碼):


function traverseAndSetDisabled(obj) {

  if(Array.isArray(obj)) {

    for(child in obj) obj[child] = traverseAndSetDisabled(child)

  } else {

    obj.isDisabled = true;

  }


  return obj;

}

[更新]

工作解決方案(結果與用戶要求的結果 100% 相同)


function traverseAndSetDisabled(obj) {

  if(typeof obj === 'object') {

    for(const child in obj) obj[child] = traverseAndSetDisabled(obj[child])

    if(!Array.isArray(obj)) {

      obj.isDisabled = true;

    }

  }


  return obj;

}


查看完整回答
反對 回復 2023-03-24
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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