0

I have this JSON object returned and I'm trying to parse it to get the value in identifier. Here's what the object looks like if I console.log it:

Object {rows: Array[33], time: 0.015, fields: Object, total_rows: 33}
fields: Object
rows: Array[33]
  0: Object
       cartodb_id: 28
       coordinates: "41.959836,-87.681545"
       created_at: "2015-03-05T14:40:43Z"
       id: "28"
       identifier: "Store Number: 28"
  1: Object
  2: Object

Here's how I'm trying to parse it:

$.getJSON('https://URLHERE, function(data) {
            console.log(data);
            var ticks6 = data.rows.map(function() {
                return data.rows.identifier;
            });

ticks6 returns an array with all values as "undefined". I've tried a bunch of different combinations here and can't get it to parse out the right piece.

1
  • data.rows.identifier is really undefined, What do you expect? Commented Mar 25, 2015 at 16:41

2 Answers 2

3
$.getJSON('https://URLHERE', function(data) {
    console.log(data);
    var ticks6 = data.rows.map(function(ele) {
        return ele.identifier;
    });
    console.log(ticks6);
});

Inside the .map function, you were accessing data.rows, that's the entire array. You want to access ele (the 1st parameter passed to the callback), which is each element in the array.

Sign up to request clarification or add additional context in comments.

3 Comments

This did the trick. Thanks! Any guesses why the data is returned in a somewhat random order? Store # 28-33 then 1-11 then 13-27 then 12. They are in order 1-33 in my table
@jonmrich: How are you making that JSON? Did you add an ORDER BY in your query?
Without an ORDER BY, MySQL doesn't know how to order your rows.
1

Array.prototype.map takes a function which is run against each item in the array. The return value is what is saved in that slot of the array. So you probably meant to write:

var ticks6 = data.rows.map(function(row) {
    return row.identifier;
});

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.