मेरे पास ऑब्जेक्ट की यह सरणी है, मुझे लूप का उपयोग करके 'डिफ़ॉल्ट_इमेज' या 'लेबल' या 'एचटीएमएल' मानों का चयन करने की आवश्यकता है। लेकिन ऐसा करने में असमर्थ।

var panel_array = [{
    "Panel":{
        "Panel_1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "Panel_2":{
            "default_image":"<img src='"+themeUrl+"/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
    "List":{
        "l1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "l2":{
            "default_image":"<img src='"+themeUrl+"/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
}]

मैंने यह कोशिश की और यह सभी मानों को प्रिंट करता है लेकिन एकल मान का उपयोग कैसे करें जैसे कि मुझे केवल 'लेबल' चाहिए या मुझे केवल 'एचटीएमएल' चाहिए:

panel_array.map(data => {
                for (var items in data) {
                    for(var dt in data[items]){
                        var ab = data[items];
                        for(var xx in ab) { 
                            var ii = ab[xx];
                            for(var oo in ii){ 
                                console.log(ii[oo]); 
                            }
                        }
                    }
                }
            })
2
wiz khalifa 27 मार्च 2018, 19:32

3 जवाब

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

आप forEachसरणी का, Object.keys और Object.values()

const panel_array = [{
  "Panel": {
    "Panel_1": {
      "default_image": "http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
      "label": "Panel List 1",
      "html": "<dl class='drop_list1'></dl>"
    },
    "Panel_2": {
      "default_image": "<img src='/prepengine/images/Interactive-items/panel2.png'",
      "label": "Panel List 2",
      "html": "<dl class='drop_list2'></dl>"
    }
  },
  "List": {
    "l1": {
      "default_image": "http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
      "label": "Panel List 1",
      "html": "<dl class='drop_list1'></dl>"
    },
    "l2": {
      "default_image": "<img src='/prepengine/images/Interactive-items/panel2.png'",
      "label": "Panel List 2",
      "html": "<dl class='drop_list2'></dl>"
    }
  },
}];


panel_array.forEach(o => {
  Object.keys(o).forEach(key => {
    Object.values(o[key]).forEach(item => {
      //you can access whatever you need
      console.log(item);
    });
  });
});
1
Narendra Jadhav 27 मार्च 2018, 17:31
let themeUrl = 'MockmyUrl';
let panel_array = [{
    "Panel":{
        "Panel_1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "Panel_2":{
            "default_image":"<img src="+themeUrl+"/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
    "List":{
        "l1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "l2":{
            "default_image":"<img src='"+themeUrl+"/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
}];
let images = [];
let lables = [];
let htmls = [];
let output  = panel_array.forEach((ele) => {
  Object.values(ele).forEach((ele2) => {
    Object.values(ele2).forEach((ele3) => {
      let {default_image, label, html} = ele3;
      images.push(default_image);
      lables.push(label);
      htmls.push(html);
    });
  });
});
console.log(images);
console.log('----------------');
console.log(lables);
console.log('----------------');
console.log(htmls);
0
AZ_ 27 मार्च 2018, 17:46

बस एक if clause जोड़ें:

var panel_array = [{
    "Panel":{
        "Panel_1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "Panel_2":{
            "default_image":"<img src='/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
    "List":{
        "l1":{
            "default_image":"http://s3.amazonaws.com/jigyaasa_content_static/interactive_item/panel_1.png",
            "label":"Panel List 1",
            "html":"<dl class='drop_list1'></dl>"
        },
        "l2":{
            "default_image":"<img src='/prepengine/images/Interactive-items/panel2.png'",
            "label":"Panel List 2",
            "html":"<dl class='drop_list2'></dl>"
        }
    },
}]
panel_array.map(data => {
   for (var items in data) {
     for(var dt in data[items]){
       var ab = data[items];
       for(var xx in ab) { 
           var ii = ab[xx];
           for(var oo in ii){ 
             if (oo == "label" || oo == "html") console.log(ii[oo]); 
           }
       }
     }
   }
})
2
messerbill 27 मार्च 2018, 16:37