Tengo un árbol por el que quiero atravesar. Quiero hacer cada internalChecked = false. Escribí un código, pero fallé.

Árbol

let json = [
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Steel",
    "value": 2,
    "internalChildren": [
      {
        "internalDisabled": false,
        "internalChecked": true,
        "internalCollapsed": false,
        "text": "Cars",
        "value": 54,
        "internalChildren": [
          {
            "internalDisabled": false,
            "internalChecked": true,
            "internalCollapsed": false,
            "text": "Sedan",
            "value": 55,
            "internalChildren": [
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "test",
                "value": 1053
              },
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "cc cc cc",
                "value": 1054,
                "internalChildren": [
                  {
                    "internalDisabled": false,
                    "internalChecked": true,
                    "internalCollapsed": false,
                    "text": "cccccc",
                    "value": 1055
                  },
                  {
                    "internalDisabled": false,
                    "internalChecked": true,
                    "internalCollapsed": false,
                    "text": "xxxxxxx",
                    "value": 1056
                  }
                ]
              }
            ]
          },
          {
            "internalDisabled": false,
            "internalChecked": true,
            "internalCollapsed": false,
            "text": "train",
            "value": 2053,
            "internalChildren": [
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "bullet",
                "value": 2054
              },
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "pessenger",
                "value": 2055
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Auto/Boat",
    "value": 3
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Build Your Own Job",
    "value": 4
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Cleaning & Housekeeping",
    "value": 5
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Delivery & Courier",
    "value": 6
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Handyman",
    "value": 7
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Hourly Help",
    "value": 8
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Lawn & Yard",
    "value": 10
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Moving",
    "value": 11
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Organization",
    "value": 12
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Painting",
    "value": 13
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Pet Care",
    "value": 14
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "TV Mount & Electronics",
    "value": 15
  }
];


//CODE
let savedIndex
let outPut= []
let savedJson = json

function traverseArray(__json, __index){
    for (let i = 0; i < __json.length; i++) {
        const _item = __json[i];
        _item.internalChecked = false
        outPut.push({
            ..._item
        })
        if(_item.internalChildren && _item.internalChildren.length > 0){
            savedIndex = __index
            traverseArray(_item.internalChildren, i)
        }
    }
    return outPut
}

console.log(traverseArray(json, 0))
0
Nadeem Ahmad 30 oct. 2019 a las 14:26

4 respuestas

La mejor respuesta

Está presionando cada fragmento json en una matriz de salida que hace que la salida sea diferente de lo que es, simplemente use __json[i].internalChecked = false en su bucle y actualizará la matriz en la que está iterando:

let json = [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Steel", "value": 2, "internalChildren": [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "Cars", "value": 54, "internalChildren": [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "Sedan", "value": 55, "internalChildren": [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "test", "value": 1053 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "cc cc cc", "value": 1054, "internalChildren": [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "cccccc", "value": 1055 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "xxxxxxx", "value": 1056 } ] } ] }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "train", "value": 2053, "internalChildren": [ { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "bullet", "value": 2054 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": false, "text": "pessenger", "value": 2055 } ] } ] } ] }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Auto/Boat", "value": 3 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Build Your Own Job", "value": 4 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Cleaning & Housekeeping", "value": 5 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Delivery & Courier", "value": 6 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Handyman", "value": 7 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Hourly Help", "value": 8 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Lawn & Yard", "value": 10 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Moving", "value": 11 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Organization", "value": 12 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Painting", "value": 13 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "Pet Care", "value": 14 }, { "internalDisabled": false, "internalChecked": true, "internalCollapsed": true, "text": "TV Mount & Electronics", "value": 15 } ];


let savedIndex
let savedJson = json

function traverseArray(__json, __index){
    for (let i = 0; i < __json.length; i++) {
        __json[i].internalChecked = false
        if (__json[i].internalChildren) {
            savedIndex = __index
            traverseArray(__json[i].internalChildren, i)
        }
    }
    return savedJson
}

console.log("original length:: ", json.length);
const output = traverseArray(savedJson, 0);
console.log("output length:: ", output.length);
 
console.log("output:: ", output);

Además, si la cláusula,

if (__json[i].internalChildren && __json[i].internalChildren.length > 0)

Puede ser simplemente

if (__json[i].internalChildren)
1
Bilal Siddiqui 30 oct. 2019 a las 11:49

Podría adoptar un enfoque recursivo y tomar una copia del objeto.

function traverseArray(array) {
    return array.map(o => Object.assign(
        {},
        o,
        { internalChecked: false },
        o.internalChildren && { internalChildren: traverseArray(o.internalChildren) }
    ));
}

let data = [{ internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Steel", value: 2, internalChildren: [{ internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "Cars", value: 54, internalChildren: [{ internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "Sedan", value: 55, internalChildren: [{ internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "test", value: 1053 }, { internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "cc cc cc", value: 1054, internalChildren: [{ internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "cccccc", value: 1055 }, { internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "xxxxxxx", value: 1056 }] }] }, { internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "train", value: 2053, internalChildren: [{ internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "bullet", value: 2054 }, { internalDisabled: false, internalChecked: true, internalCollapsed: false, text: "pessenger", value: 2055 }] }] }] }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Auto/Boat", value: 3 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Build Your Own Job", value: 4 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Cleaning & Housekeeping", value: 5 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Delivery & Courier", value: 6 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Handyman", value: 7 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Hourly Help", value: 8 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Lawn & Yard", value: 10 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Moving", value: 11 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Organization", value: 12 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Painting", value: 13 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "Pet Care", value: 14 }, { internalDisabled: false, internalChecked: true, internalCollapsed: true, text: "TV Mount & Electronics", value: 15 }];

console.log(traverseArray(data, 0));
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
Nina Scholz 30 oct. 2019 a las 11:56

Esta es una solución recursiva, no la mejor para la eficiencia, ya que podría hacerse recursiva.

let json = [
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Steel",
    "value": 2,
    "internalChildren": [
      {
        "internalDisabled": false,
        "internalChecked": true,
        "internalCollapsed": false,
        "text": "Cars",
        "value": 54,
        "internalChildren": [
          {
            "internalDisabled": false,
            "internalChecked": true,
            "internalCollapsed": false,
            "text": "Sedan",
            "value": 55,
            "internalChildren": [
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "test",
                "value": 1053
              },
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "cc cc cc",
                "value": 1054,
                "internalChildren": [
                  {
                    "internalDisabled": false,
                    "internalChecked": true,
                    "internalCollapsed": false,
                    "text": "cccccc",
                    "value": 1055
                  },
                  {
                    "internalDisabled": false,
                    "internalChecked": true,
                    "internalCollapsed": false,
                    "text": "xxxxxxx",
                    "value": 1056
                  }
                ]
              }
            ]
          },
          {
            "internalDisabled": false,
            "internalChecked": true,
            "internalCollapsed": false,
            "text": "train",
            "value": 2053,
            "internalChildren": [
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "bullet",
                "value": 2054
              },
              {
                "internalDisabled": false,
                "internalChecked": true,
                "internalCollapsed": false,
                "text": "pessenger",
                "value": 2055
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Auto/Boat",
    "value": 3
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Build Your Own Job",
    "value": 4
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Cleaning & Housekeeping",
    "value": 5
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Delivery & Courier",
    "value": 6
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Handyman",
    "value": 7
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Hourly Help",
    "value": 8
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Lawn & Yard",
    "value": 10
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Moving",
    "value": 11
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Organization",
    "value": 12
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Painting",
    "value": 13
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "Pet Care",
    "value": 14
  },
  {
    "internalDisabled": false,
    "internalChecked": true,
    "internalCollapsed": true,
    "text": "TV Mount & Electronics",
    "value": 15
  }
];


//CODE
let savedIndex
let outPut= []
let savedJson = json

function traverseArray(arr){
    try{
      arr.forEach((obj) => {
        if(obj.internalChecked){
           obj.internalChecked = false
        }
        Object.keys(obj).forEach((k)=>{
          if(obj[k].length>1){
              traverseArray(obj[k]);
           }
        });
      });
    }catch(e){
      if(arr.internalChecked){
           arr.internalChecked = false
       }
    }
      
}

traverseArray(json);
console.log(json)
0
Greedo 30 oct. 2019 a las 11:40

Puede crear un método recursivo getTraversedArray usando Array.prototype.map ()

Código:

const json = [{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Steel',value: 2,internalChildren: [{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'Cars',value: 54,internalChildren: [{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'Sedan',value: 55,internalChildren: [{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'test',value: 1053,},{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'cc cc cc',value: 1054,internalChildren: [{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'cccccc',value: 1055,},{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'xxxxxxx',value: 1056,},],},],},{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'train',value: 2053,internalChildren: [{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'bullet',value: 2054,},{internalDisabled: false,internalChecked: true,internalCollapsed: false,text: 'pessenger',value: 2055,},],},],},],},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Auto/Boat',value: 3,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Build Your Own Job',value: 4,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Cleaning & Housekeeping',value: 5,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Delivery & Courier',value: 6,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Handyman',value: 7,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Hourly Help',value: 8,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Lawn & Yard',value: 10,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Moving',value: 11,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Organization',value: 12,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Painting',value: 13,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'Pet Care',value: 14,},{internalDisabled: false,internalChecked: true,internalCollapsed: true,text: 'TV Mount & Electronics',value: 15,},]

const getTraversedArray = (json) => json.map(o => {
  o.internalChecked = false
  if (Array.isArray(o.internalChildren)) {
    o.internalChildren = getTraversedArray(o.internalChildren)
  }
  return o
})

console.log(getTraversedArray(json))
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
Yosvel Quintero 30 oct. 2019 a las 11:49