0

I created dropdown and get value from the response, but here is the issue, reverse.config have two value:1 ,2. now I have to print 2, 1 which I am trying to print, I tried using reverse(), but its not working.

jQuery.each(response.config, function (index,value) {
    var select = value.label + '<select class="configurable"  name="' + index + '" >';
    jQuery.each(value.values, function (key, opt) {
        select += '<option value="' + opt.value_index + '">' + opt.label + '</option>>'
    });
    select += '</select>';
    jQuery("#newProduct").append(select);
});
14
  • 2
    Try value = response.config[response.config.length - index - 1] Commented Nov 26, 2019 at 5:34
  • response.config is Array or Object ? Commented Nov 26, 2019 at 5:36
  • @JayVaghasiya object Commented Nov 26, 2019 at 5:41
  • 1
    I guess this might help you stackoverflow.com/a/4838642/5513005 Commented Nov 26, 2019 at 6:03
  • 1
    @SagarParikhSGR From your Object there are 2 keys 93 and 141 so you want 141 and 93, right ? Commented Nov 26, 2019 at 6:32

2 Answers 2

1

value.values is your object. you can get your keys using Object.keys(value.values) then reverse it now you have keys in reversed order. iterate over it and get you object base on reversed keys array. According to your json asuming below json is same as your format.

var json={
  "41": {
    "values": [
      {
        "valeu_index": 1,
        "label": "op1"
      },
      {
        "valeu_index": 2,
        "label": "op2"
      }
    ],
    "label": "lbl1"
  },
  "91": {
    "values": [
      {
        "valeu_index": 1,
        "label": "opt1"
      },
      {
        "valeu_index": 2,
        "label": "opt2"
      }
    ],
    "label": "lbl2"
  }
}

var values1=[{valeu_index:1,label:"op1"},{valeu_index:2,label:"op2"}]
var values2=[{valeu_index:1,label:"opt1"},{valeu_index:2,label:"opt2"}]
var response={};
response[41]={values:values1,label:'lbl1'};
response[91]={values:values2,label:"lbl2"};
console.log(response);
 var select = '<select class="configurable"  name="config" >';
var reversed=Object.keys(response).reverse(); 

        $.each(reversed, function (key, opt) {
                var obj=response[opt];
                var select = obj.label+'<select class="configurable"  name="config" >';
                debugger;
                $.each(obj.values,function(i,o){
                select += '<option value="' + o.value_index + '">' + o.label + '</option>>'
                });
                 select += '</select>';
            $("#newProduct").append(select);

            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='newProduct'></div>

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use reverse on Array

var t = {
  93: { id: 3 },
  141: { id: 2 }
}

jQuery.each(Object.keys(t).reverse(), function( index, value ) {
  console.log(t[value]);
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.