最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop through jquery data() object to get keys and values - Stack Overflow

programmeradmin3浏览0评论

I have a data() object containing some json.

Is there a way I can loop through the object and grab each parts key and value?

This is what I have so far:

function getFigures() {

var propertyValue = getUrlVars()["propertyValue"];

$.getJSON(serviceURL + 'calculator.php?value=' + propertyValue, function(data) {

    figures = data.figures;
    $.each(figures, function(index, figure) {

        $('#figureList').append('<li> index = ' + data.figures.figure + '</li>');

    });

});

$('#figureList').listview('refresh');

}

The json looks like this:

{"figures":{"value":"150000","pletion":"10.00","coal":"32.40","local":"144.00","bacs":"35.00","landRegistry":"200.00","solFee":"395.00","vatOnSolFees":79,"stampDuty":1500,"total":2395.4}}

Apologies if its simple, I'm new to jQuery and couldn't find anything on SO that helped.

I have a data() object containing some json.

Is there a way I can loop through the object and grab each parts key and value?

This is what I have so far:

function getFigures() {

var propertyValue = getUrlVars()["propertyValue"];

$.getJSON(serviceURL + 'calculator.php?value=' + propertyValue, function(data) {

    figures = data.figures;
    $.each(figures, function(index, figure) {

        $('#figureList').append('<li> index = ' + data.figures.figure + '</li>');

    });

});

$('#figureList').listview('refresh');

}

The json looks like this:

{"figures":{"value":"150000","pletion":"10.00","coal":"32.40","local":"144.00","bacs":"35.00","landRegistry":"200.00","solFee":"395.00","vatOnSolFees":79,"stampDuty":1500,"total":2395.4}}

Apologies if its simple, I'm new to jQuery and couldn't find anything on SO that helped.

Share Improve this question edited Dec 24, 2015 at 9:38 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 17, 2012 at 13:38 JimotheyJimothey 2,4349 gold badges42 silver badges68 bronze badges 2
  • What do you expect to get inside #figureList? – VisioN Commented Jun 17, 2012 at 13:40
  • Try this: [stackoverflow./questions/684672/… [1]: stackoverflow./questions/684672/… – Jesús Quintana Commented Jun 17, 2012 at 13:43
Add a ment  | 

3 Answers 3

Reset to default 5

You can get the key and value like this

$.each(data.figures, function(key, val) {

        console.log('Key: ' + key + '  Val: ' + val)

    });​

So change your code to

 $('#figureList').append('<li>'+ index + ' = ' + figure + '</li>');

Demo: http://jsfiddle/joycse06/ERAgu/

The parameters index and figure contains the parameter name and value. I think that you want to concatenate the parameters into the string:

$('#figureList').append('<li>' + index + ' = ' + figure + '</li>');

An alternative is to create the list item element and set the text of it, that would also work if the text contains any characters that need encoding:

$('#figureList').append($('<li/>').text(index + ' = ' + figure));
function getFigures() {

   var propertyValue = getUrlVars()["propertyValue"];

   $.getJSON(serviceURL + 'calculator.php?value=' + propertyValue, function(data) {

       $.each(data['figures'], function(index, val) {

          here grab "val" is key 

          $.each(data['figures'][index], function(col, valc) {

           here grab "col" is value

          }
       }

   }      

bye

发布评论

评论列表(0)

  1. 暂无评论