0

I am trying to get the array value from the object I created but seems like that obj.item is not working to me. It always saying undefined here's my code::

$.each(componentSentenceArray, function(index,item) { 
          conole.log(item.item_name)

});

and here is my object

ingredient:
Array(3)
0:{item_name: "Albenza", uom_code: "", uom_desc: "", amount: ""}
1:{item_name: "Baclofen", uom_code: "", uom_desc: "", amount: ""}
2:{item_name: "Lasix", uom_code: "", uom_desc: "", amount: ""}


main_component:
Array(1)
0 :{item_name: "Lasix", uom_code: "", uom_desc: "", amount: ""}
7
  • 2
    Please post your actual code, instead of an image. And, I cannot find item key anywhere in the snapshot. Commented Nov 13, 2017 at 5:29
  • What are your array items? What are you trying to get? Commented Nov 13, 2017 at 5:30
  • @Mamun I updated the image bro. Commented Nov 13, 2017 at 5:35
  • @31piy I cannot replicate the actual code because the data is coming from DB Commented Nov 13, 2017 at 5:35
  • @kevin_marcus -- yes you can. Observe the network tab of your browser's developer tools. Copy the response of the API request and attach it in the question. Commented Nov 13, 2017 at 5:37

3 Answers 3

2

$(document).ready(function() {
  var componentSentenceArray = {
    ingredient: [{
        item_name: "Albenza",
        uom_code: "",
        uom_desc: "",
        amount: ""
      },
      {
        item_name: "Baclofen",
        uom_code: "",
        uom_desc: "",
        amount: ""
      },
      {
        item_name: "Lasix",
        uom_code: "",
        uom_desc: "",
        amount: ""
      }
    ],
    main_component: [{
      item_name: "Lasix",
      uom_code: "",
      uom_desc: "",
      amount: ""
    }]
  };

  $.each(componentSentenceArray, function(index, item) {
    $.each(item, function(index, item1) {
      console.log(item1.item_name);
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

This should work for you. You are having array list inside and object which you should loop again to access its variable.

  $.each(componentSentenceArray, function(index,item) { 
    $.each(item, function(index,item1) { 
      console.log(item1.item_name);    
    });
  });
Sign up to request clarification or add additional context in comments.

Comments

2
$.each(componentSentenceArray, function (key, data) {
    console.log(key)
    $.each(data, function (index, data) {
        console.log('index', data)
    })
})

this will work!

Comments

1
$.each(componentSentenceArray, function(index,item) { 
     $.each(item, function(index, arrayItem) {
       console.log(arrayItem.item_name)
     })
});

This should help

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.