Reputation: 115
I have a Name Value pair returned from Javascript array which will be in this format
var priceData = [[0,100.34],[1,108.31],[2,109.40],[3,104.87],[4,106.00]]
I need the last element in that array , so i used this way
var result = priceData[priceData.length-1];
alert(result);
But i am getting result as 4,106.00
I want to get only the 106.00 (That is the last elemenet)
Please tell me , how to do this ??
Upvotes: 0
Views: 594
Reputation: 91094
> x=[[1,2],[3,4],[5,6]]
> x.slice(-1)[0]
[5,6]
> x.slice(-1)[0].slice(-1)[0]
6
alternatively:
> x.slice(-1)[0][1]
6
Upvotes: 2
Reputation: 13590
var priceData = [[0,100.34],[1,108.31],[2,109.40],[3,104.87],[4,106.00]]
var result = priceData[priceData.length-1];
alert(result[result.length-1]);
Upvotes: 1
Reputation: 32429
The last element of your array is indeed [4, 106.00]. The last element of the last element of your array is 106.00
Upvotes: 1
Reputation: 40224
It's another array, so index the 1
-th element:
var result = priceData[priceData.length-1][1];
Upvotes: 1