0

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 ??

4 Answers 4

2
> 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
Sign up to request clarification or add additional context in comments.

1 Comment

x.slice(-1)[0].slice(-1)[0] = 6
1

It's another array, so index the 1-th element:

var result = priceData[priceData.length-1][1];

1 Comment

this code gives you the second element in that array not the last. just that in this example the second item happens to be the last.
1

The last element of your array is indeed [4, 106.00]. The last element of the last element of your array is 106.00

Comments

1
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]);

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.