English 中文(简体)
Getting the last value of an Array and showing it under X axis
原标题:
var NewdateData[] = [1,2,3,4,5,6,7,8,9,1,2,1,23,45,56]

This NewdateData is dynamically filled from database depending upon the selection made from the user interface.

I am using this NewdateData for displaying under the X axis Charts.

The issue I am facing is that, the values are not taken till the end , I want to have the last value to have under the X axis Labels.

xaxis: {tickFormatter: function(n)
{
    var k = Math.round(n);
    return NewdateData[k]; 
}

I am using flotr.

问题回答

You can get the last value of an array with:

NewdateData[NewdateData.length-1];

Very late to the party, but for posterity: in ES2015/ES6 you can use Array.prototype.slice. Doesn t mutate the array and a negative number gives you elements from the end of the array as a new array.

So to get the last element:

let arr = [1, 2, 3, 4, 5];
let last = arr.slice(-1); // last = 5      

I don’t have enough points to comment on Radman’s post., but his solution is wrong.

let arr = [1, 2, 3, 4, 5]; let last = arr.slice(-1); // last = 5

Returns [5], not 5.

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

The correct answer:

let arr = [1, 2, 3, 4, 5];
let last = arr.slice(-1)[0];

References: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Just do it with the map function.

this is the array what I want to pick the last item from it:-

const animals = [ Dodo ,  Tiger ,  Penguin ,  Dodo ];

loop over the array using map function to use the index parameter and compare it with animals array length:-

animals.map((animal, index) => animals.length -1 === index ? console.log("last item selected :)" + animal) : console.log("i m not the last item"))

Now we are living with ES6 features

var arr = [1,2,3,4,5,6]

const [a, ...b] = arr.reverse();

console.log(a)

A simple and convenient way is to use Array.prototype.at()

function returnLast(arr) {
  return arr.at(-1);
}

const cart = [ apple ,  banana ,  pear ];
const lastItem = returnLast(cart);

console.log(lastItem)  //pear

or just

const lastItem = cart.at(-1)




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签