简体   繁体   English

为对象中的多个数组分配相同的值

[英]Assign same value to multiple array in object

Hi i am trying to assign a single value to multiple dynamic array and nested array in single object.嗨,我正在尝试将单个值分配给单个对象中的多个动态数组和嵌套数组。

here is here object:这是这里的对象:

 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", }, ] }

Below is the o/p下面是 o/p

 object = { 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: [] }, { id: 1, text: 'composit3', isDisabled: true, compositeConfig: [] }, ], id: 3576, tenantId: "1", }, ] }

In the Above object a new value is assigned that is "isDisabled: true" which is assigned to all the arrays as well as inner or nested array.在上面的对象中,分配了一个新值“isDisabled:true”,该值分配给所有数组以及内部或嵌套数组。

How to assign value to multiple dynamic arrays no matter how many arrays are there or nested arrays无论有多少数组或嵌套数组,如何为多个动态数组赋值

There's no magic way there.那里没有神奇的方法。
You should just write simple recursion.你应该只写简单的递归。
Something like this should work (pseudocode):这样的事情应该工作(伪代码):

function traverseAndSetDisabled(obj) {
  if(Array.isArray(obj)) {
    for(child in obj) obj[child] = traverseAndSetDisabled(child)
  } else {
    obj.isDisabled = true;
  }

  return obj;
}

[UPDATE] [更新]

Working solution (result is 100% the same as user requested)工作解决方案(结果与用户要求的 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;
}
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;
  }
 }
})

Edit: Missed the "nested arrays" part in the question.编辑:错过了问题中的“嵌套数组”部分。

Try this :试试这个

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;
  }
}

Usage用法

setProperty(object, "isDisabled", true);
setProperty(object, "isDisabled", false);
setProperty(object, "someKey", "Some Value");

Run the following code snippet for a demo运行以下代码片段进行演示

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM