4

I'm trying to access all brands and values for dropdown menu but I couldn't that with this way.

<select id="secim">
</select>

var data = [
        {
          "products": "Cars",
          "brands_values" : [
            {"brand":"fiat","value":1},
            {"brand":"bmw","value":2}
          ]
      }
      ];

$.each(data, function(i, item) {
        $('#secim').append($('<option>', {
          value: item.brands_values.value,
          text: item.brands_values.brand
        }));
      });

How could I do? Thank you

1
  • 2
    This issue here is that brands_values is an array with multiple values. You would need an additional loop with your $.each to iterate over the elements within brands_values. Commented Aug 17, 2017 at 11:21

4 Answers 4

3

Just add another loop for the brands:

var data = [
  {
    "products": "Cars",
    "brands_values" : [
      {"brand":"fiat","value":1},
      {"brand":"bmw","value":2}
    ]
}];

$.each(data, function(i, item) {
  if (item.brands_values) {
      item.brands_values.forEach(brands => {
        $('#secim').append($('<option>', {
          value: brands.value,
          text: brands.brand
        }));
      });
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="secim">
</select>

Note: You may want to use native .forEach in that case, since it is enough. I had performance issues with jQuery.each() in the past so I avoid it whenever I can(check this):

data.forEach(item => {
    if (item.brands_values) {
        item.brands_values.forEach(brands => {
            $('#secim').append($('<option>', {
                value: brands.value,
                text: brands.brand
            }));
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Works but why two loops ??
@ucnumara glad to help!!
2
$.each(data, function(i, item) {
  $.each(item.brands_values, function(j, brand){
    $('#secim').append($('<option>', {
      value: brand.value,
      text: brand.brand
    }));
  });
});

You need another iteration - of course the first "loop" is not very elegant, but this is due to the given data format.

Working Fiddle: https://jsfiddle.net/3pL6n6pj/

Comments

0

First create a list of brand_values from data by using a #reduce() function. Then pass it as items to your $.each.

See demo below:

var data = [{
  "products": "Cars",
  "brands_values": [{
      "brand": "fiat",
      "value": 1
    },
    {
      "brand": "bmw",
      "value": 2
    }
  ]
}];

var items = data.reduce(function(p,c){
  c.brands_values.forEach(function(e){
    p.push(e);
  });
  return p;
},[]);

$.each(items, function(i,item) {
  $('#secim').append($('<option>', {
    value: item.value,
    text: item.brand
  }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="secim"></select>

Comments

0

Iterate over brand values and not just data. So that you can acces each brand and value.

  var data = [
        {
          "products": "Cars",
          "brands_values" : [
            {"brand":"fiat","value":1},
            {"brand":"bmw","value":2}
          ]
      }
      ];

  
  $.each(data[0].brands_values, function(i, item) {
       $('#secim').append($('<option>', {
              value: item.value,
              text: item.brand
            }));
          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="secim">
</select>

1 Comment

IMO it's a good practice to check for brands_values value, it could be null. Also, if data.length > 0 as you're hard-coding it's index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.