0

Please help me to iterate the below array in jquery and get each value .

var data = [{"value":"150000","completion":"10.00","coal":"32.40","local":"144.00","bacs":"35.00","landRegistry":"200.00","solFee":"395.00","vatOnSolFees":79,"stampDuty":1500,"total":2395.4},{"value":"120000","completion":"1.00","coal":"3.40","local":"14.00","bacs":"3.00","landRegistry":"100.00","solFee":"35.00","vatOnSolFees":7,"stampDuty":150,"total":395.4}];
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
user6773905
  • 105
  • 1
  • 1
  • 8
  • 2
    use a loop man a loop :) – madalinivascu Feb 09 '17 at 11:09
  • See also: http://stackoverflow.com/questions/19529403/javascript-loop-through-object-array, and http://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties. There are a *lot* of questions on this already if you search – Rory McCrossan Feb 09 '17 at 11:14

1 Answers1

0

Use a jquery each to loop through the elements of the array

    var data = [{"value":"150000","completion":"10.00","coal":"32.40","local":"144.00","bacs":"35.00","landRegistry":"200.00","solFee":"395.00","vatOnSolFees":79,"stampDuty":1500,"total":2395.4},{"value":"120000","completion":"1.00","coal":"3.40","local":"14.00","bacs":"3.00","landRegistry":"100.00","solFee":"35.00","vatOnSolFees":7,"stampDuty":150,"total":395.4}];
    
    $.each(data,function(i,v){
       console.log(v.value);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

if you want to loop over the objects add another loop

var data = [{
  "value": "150000",
  "completion": "10.00",
  "coal": "32.40",
  "local": "144.00",
  "bacs": "35.00",
  "landRegistry": "200.00",
  "solFee": "395.00",
  "vatOnSolFees": 79,
  "stampDuty": 1500,
  "total": 2395.4
}, {
  "value": "120000",
  "completion": "1.00",
  "coal": "3.40",
  "local": "14.00",
  "bacs": "3.00",
  "landRegistry": "100.00",
  "solFee": "35.00",
  "vatOnSolFees": 7,
  "stampDuty": 150,
  "total": 395.4
}];


$.each(data, function(i, v) {
  $.each(v, function(index, value) {
    console.log(index + ':' + value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • i tried the above code but i am not able to go inside loop and no error is generated – user6773905 Feb 09 '17 at 11:32
  • see the console for errors – madalinivascu Feb 09 '17 at 11:37
  • Hey i want one more help from u bro i want to add edit and delete button in below code of html table .... I am creating table based on JSON now i want to add two column in the same table Edit and Delete . PLease Help – user6773905 Feb 11 '17 at 09:48
  • Please find code Below : – user6773905 Feb 11 '17 at 09:51
  • My Json : function CreateTableFromJSON() { var myBooks = [ { "Book ID": "1", "Book Name": "Computer Architecture", "Category": "Computers", "Price": "125.60" }, { "Book ID": "2", "Book Name": "Asp.Net 4 Blue Book", "Category": "Programming", "Price": "56.00" }, ] – user6773905 Feb 11 '17 at 09:52
  • var col = []; for (var i = 0; i < myBooks.length; i++) { for (var key in myBooks[i]) { if (col.indexOf(key) === -1) { col.push(key); } } } // CREATE DYNAMIC TABLE. var table = document.createElement("table"); var tr = table.insertRow(-1); for (var i = 0; i < col.length; i++) { var th = document.createElement("th"); th.innerHTML = col[i]; tr.appendChild(th); } – user6773905 Feb 11 '17 at 09:53
  • @user6773905 create another question so we can help – madalinivascu Feb 11 '17 at 14:53