0

My json returns below shown set of array of objects and I need to access the data inside it. In the console this is what the response looks like enter image description here

What i am trying to do is to access the object to get the subcategoryid and subcategoryname then display it inside a dropdown list. Here is my code for it

$.get('ajax-subcat?cat_id=' + cat_id, function(data)
          {
              console.log(data);

              $('#subcategory').empty();

              $.each(data, function(index, subcatObj)
              {
                alert(subcatObj);
                $('#subcategory').append('<option value="' + subcatObj.Object.subcategoryid +'">' + subcatObj.subcategoryname +'</option>');
              });

          });

The thing I don't know is how to access the data inside the object. Any ideas?

2
  • Just replace subcatObj.Object.subcategoryid with subcatObj.subcategoryid Commented Jul 21, 2016 at 6:23
  • hi @PrakashThete i tried it but did'nt work Commented Jul 21, 2016 at 6:35

3 Answers 3

2

Try this:

JAVASCRIPT

for (var i = 0; i < data.length; i++) {
    console.log(data[i].subcategoryid);
}
Sign up to request clarification or add additional context in comments.

4 Comments

hello why is it logging twice?
Hi, in what manner is it logging twice? Can you maybe provide me with an example from the logged output in the console then I can maybe tell you why?
hm for example i have subcategoryid 7,8,9 in the console it is being logged 3 times. i have this onchange and the isTrigger value is 3 maybe that causes the error
Okay that might be the issue, but I am not quite sure. The following reasons can cause the data to log more than once: 1. Multiple Objects with same value, 2. ;$.get' gets bound more than once, 3. The console.log is called more than once. Maybe update your answer with an example of current output with the example I gave and your full code or function so we can determine what is causing the issue.
1

Assuming that you have a select element in markup:

<select id="mySelectElement"></select>

Try this to iterate the object and fill the combo box:

$.get('ajax-subcat?cat_id=' + cat_id, function(data) {
   // Get a reference to the target element
   var selectTarget = $('#mySelectElement');
   // Iterate over the response data
   for (var i in data) {
      // For every object in array, append a new option element
      selectTarget.append($('<option value="' + data[i].subcategoryid + '">' + data[i].subcategoryname + '</option>'));
   }
});

1 Comment

hi i did this but it was not able to populate the dropdown? any ideas?
1

For this purpose, you can use underscore.js library to get whole data corresponding to particular key in the form of array.

_.pluck(data, 'subCategoryid') // array of all values corresponding to 'subcategoryid'

Comments

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.