मेरे पास एक पेड़ है, जिसे मैं पार करना चाहता हूं। मैं प्रत्येक internalChecked = false बनाना चाहता हूं। मैंने कुछ कोड लिखा, लेकिन असफल रहा।

पेड़

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 अक्टूबर 2019, 14:26

3 जवाब

सबसे बढ़िया उत्तर

आप आउटपुट सरणी में प्रत्येक जेसन खंड को दबा रहे हैं जो आउटपुट को इससे अलग बनाता है, बस अपने लूप में __json[i].internalChecked = false का उपयोग करें और यह उस सरणी को अपडेट करेगा जिस पर आप पुनरावृति कर रहे हैं:

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

इसके अलावा, यदि खंड,

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

सरल हो सकता है

if (__json[i].internalChildren)
1
Bilal Siddiqui 30 अक्टूबर 2019, 11:49

यह एक पुनरावर्ती समाधान है, दक्षता के लिए सबसे अच्छा नहीं है क्योंकि इसे पूंछ पुनरावर्ती बनाया जा सकता है।

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 अक्टूबर 2019, 11:40

आप एक पुनरावर्ती दृष्टिकोण ले सकते हैं और वस्तु से एक प्रति ले सकते हैं।

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 अक्टूबर 2019, 11:56