1

I have a object like this, which contain location and stopover values.

[{"location":"8, Nehru Nagar, Ambavadi, Ahmedabad, Gujarat 380015, India","stopover":true},
{"location":"CISF Cargo Road, Sardar Vallabhbhai Patel International Airport (AMD), Hansol, Ahmedabad, Gujarat 382475, India","stopover":true},
{"location":"Sardar Patel Ring Road, Sughad, Ahmedabad, Gujarat 382424, India","stopover":true},
{"location":"Kudasan Road, Urjanagar 1, Kudasan, Gujarat 382421, India","stopover":true},
{"location":"Gujarat State HIghway 141, Alampur, Gujarat 382355, India","stopover":true},
{"location":"Hanuman Ji Mandir Bus Stop, Dabhoda, Gujarat 382355, India","stopover":true}]

so my question is
(1) how to get first value of location as start destination?
(2) how to get last value of location as end destination?
(3) how to get other values of location as waypoints?

see this,how i pushed value in waypts

Community
  • 1
  • 1
DS9
  • 2,995
  • 4
  • 52
  • 102

3 Answers3

1

That's not just an object, it's an array and so the items can be accessed by index.

So if that object is assigned to a variable

  places = [{"location":"8, Nehru Nagar, Ambavadi, Ahmedabad, Gujarat 380015, India","stopover":true},
{"location":"CISF Cargo Road, Sardar Vallabhbhai Patel International Airport (AMD), Hansol, Ahmedabad, Gujarat 382475, India","stopover":true},
{"location":"Sardar Patel Ring Road, Sughad, Ahmedabad, Gujarat 382424, India","stopover":true},
{"location":"Kudasan Road, Urjanagar 1, Kudasan, Gujarat 382421, India","stopover":true},
{"location":"Gujarat State HIghway 141, Alampur, Gujarat 382355, India","stopover":true},
{"location":"Hanuman Ji Mandir Bus Stop, Dabhoda, Gujarat 382355, India","stopover":true}];

You can access

 places[0]; // first
 places[places.length -1]; // last

and iterate using

 for ( var i = 1; i < places.length - 2 ; i++){
    places[i]; // access to waypoints
 }
djna
  • 54,992
  • 14
  • 74
  • 117
  • i alert this :`alert(typeof waypts);` and it display it's an object. – DS9 Oct 16 '13 at 05:52
  • @DS9: json = JavaScript **Object** Notation – Gowri Oct 16 '13 at 05:53
  • [see this,how i pushed value in waypts](http://stackoverflow.com/questions/19380203/old-value-of-array-replace-by-new-value-of-array) – DS9 Oct 16 '13 at 05:54
  • arrays are objects, so typeof is misleading you. See http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – djna Oct 16 '13 at 05:57
  • In your code you have var waypts=[]; that's an array. Please try my answer. – djna Oct 16 '13 at 05:58
  • it's alert [object Object] – DS9 Oct 16 '13 at 06:02
1

A basic example :

var a = [{p:1},{p:2},{p:3},{p:4}];
/* first */  a[0];            // Object {p: 1}
/* last */   a[a.length - 1]; // Object {p: 4}
/* second */ a[1];            // Object {p: 2}
             a[0].p;          // 1

Don't rely on typeof :

typeof new Array // "object"
typeof new Object // "object"
1

What you've got there is an array of objects. The individual items in the array can be accessed by numeric index, and then the individual properties of each object can be accessed by name. So:

// assuming waypts is the variable/function
// argument referring to the array:

var firstLoc = waypts[0].location;
var lastLoc = waypts[waypts.length-1].location;

Bearing in mind that JS array indexes start at 0, you can get the location at position n in the array with

waypts[n].location

And of course a standard for loop lets you iterate over all the waypoints in the array:

for(var j=0; j < waypts.length; j++) {
    alert(waypts[j].location);
}

You'd access the stopover property in the same way:

waypts[j].stopover
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • thank you very much.it's work.but don't understand that i use `waypts.push({location:add,stopover:true});` to create array then how it become array of objects?. – DS9 Oct 16 '13 at 06:06
  • 1
    `{location:add,stopover:true}` is an object. So each item you push into the array is an object. – nnnnnn Oct 16 '13 at 09:26