7 Replies - 522 Views - Last Post: 22 February 2013 - 02:36 AM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

how do i add a callback function?

Posted 21 February 2013 - 05:46 PM

so i use .getJSON to get all the key/value pairs this way:

    $.getJSON("http://domain.com/results.php",function(result){
      $.each(result, function(key, value){
            ....
      });
    });


but how do i get the 1st key/value only?

This post has been edited by g37752: 21 February 2013 - 10:52 PM

Is This A Good Question/Topic? 0
  • +

Replies To: how do i add a callback function?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how do i add a callback function?

Posted 21 February 2013 - 11:15 PM

the question would be, what makes the first key the first one? (well, in an array that would be obvious ...)
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: how do i add a callback function?

Posted 21 February 2013 - 11:19 PM

View PostDormilich, on 21 February 2013 - 11:15 PM, said:

the question would be, what makes the first key the first one? (well, in an array that would be obvious ...)


so how do i get the 1st key/value of the array? result[0], key[0], value[0] dont work..

This post has been edited by g37752: 21 February 2013 - 11:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how do i add a callback function?

Posted 21 February 2013 - 11:30 PM

View Postg37752, on 22 February 2013 - 07:19 AM, said:

result[0], key[0], value[0] dont work..

then neither result, key nor value are an array.

judging from your description, your JSON returns an object (key-value pairs) and the properties (keys) of an object do not have an inherent order, hence there is no first or last key. The only thing you can get is the first key in an iteration (or loop) but you have no control of what key is chosen by the system/JS to be the first one.
Was This Post Helpful? 1
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: how do i add a callback function?

Posted 21 February 2013 - 11:43 PM

i tried a different approach:

in the .each loop, i push the value to an array:

var array=new Array();

.each loop:
array.push(value);


after the loop,
alert(array[0]);
gives undefined?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how do i add a callback function?

Posted 21 February 2013 - 11:47 PM

then there’s probably something going wrong in the loop.

btw. if you want only one result, why do you use a full JSON object? wouldn’t it be easier to let the server send only one value?
Was This Post Helpful? 1
  • +
  • -

#7 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: how do i add a callback function?

Posted 21 February 2013 - 11:53 PM

View PostDormilich, on 21 February 2013 - 11:47 PM, said:

then there’s probably something going wrong in the loop.

btw. if you want only one result, why do you use a full JSON object? wouldn’t it be easier to let the server send only one value?


i also need everything.
now for the 1st value, i would have to make a separate separate php request page?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how do i add a callback function?

Posted 22 February 2013 - 02:36 AM

if you need everything, then why bothering what the first value is?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1