how to get the last element of array and save in variable


I am trying to save the last element of an array which gets its values from a multiselect dropdown and looks like this for example with two elements:
[34, 33]

What I want to do know is to always get the last element inside it in this case the 33 and save it for further use in a variable. But for some reason it always gives me all elements like this:
[34, 33]
But I want to have just the 34 if it is the only element or the 33 if it is the last element
.

this is the part where I am trying to get the last element:

let personId = 0
    let personersonId2 = 0
    this.personArray.push(this.form.controls.legitimiertGwg.value)
    this.personArrayStaging = this.legitmiertePersonArray[length]
    console.log(this.form.controls.legitimiertGwg.value)
   
    for(personId = 0; personId < this.personArray.length - 1; personId ++){
        personId2 = this.personArray[personId ]
    }

Source: JavaSript – Stack Overflow

November 22, 2021
Category : News
Tags: angular | javascript | typescript

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.