Given a JavaScript object and the task is to get the last element of the JavaScript object.
Approach 1:
- Use Object.keys() method to get the all keys of the object.
- Now use indexing to access the last element of the JavaScript object.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to get the last item of JavaScript object ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP1" style = "font-size: 15px; font-weight: bold;"> </p> <p id = "GFG_UP2" style = "font-size: 15px; font-weight: bold; color: green;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up1 = document.getElementById('GFG_UP1'); var up2 = document.getElementById('GFG_UP2'); var down = document.getElementById('GFG_DOWN'); var Obj = { "1_prop": "1_Val", "2_prop": "2_Val", "3_prop": "3_Val" }; up1.innerHTML = "Click on the button to get" + "the last element of the Object."; up2.innerHTML = JSON.stringify(Obj); function GFG_Fun() { down.innerHTML = "The last key = '" + Object.keys(Obj)[Object.keys(Obj).length-1] + "' <br> Value = '" + Obj[Object.keys(Obj)[Object.keys(Obj).length-1]] + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Use for loop to access the all keys of the object and at the end of the loop, loop variable will have the last key of the object.
- Now use indexing to access the last element’s value of the JavaScript object.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to get the last item of JavaScript object ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP1" style = "font-size: 15px; font-weight: bold;"> </p> <p id = "GFG_UP2" style = "font-size: 15px; font-weight: bold; color: green;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up1 = document.getElementById('GFG_UP1'); var up2 = document.getElementById('GFG_UP2'); var down = document.getElementById('GFG_DOWN'); var Obj = { "1_prop": "1_Val", "2_prop": "2_Val", "3_prop": "3_Val" }; up1.innerHTML = "Click on the button to get" + "the last element of the Object."; up2.innerHTML = JSON.stringify(Obj); function GFG_Fun() { var lastElement; for (lastElement in Obj); lastElement; down.innerHTML = "The last key = '" + lastElement + "' <br> Value = '" + Obj[lastElement] + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to delete an item or object from the array using ng-click ?
- Get the first and last item in an array using JavaScript
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- JavaScript | Remove the last item from an array
- How to replace an item from an array in JavaScript?
- How to find if two arrays contain any common item in Javascript?
- How to insert an item into array at specific index in JavaScript?
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- Object.is( ) in JavaScript
- Map vs Object in JavaScript
- How to get the first key name of a JavaScript object ?
- How to get a key in a JavaScript object by its value ?
- JavaScript | Set object key by variable
- Object.entries() In JavaScript
- How to add an object to an array in JavaScript ?
- JavaScript | Proxy() Object
- Object.keys( ) In JavaScript
- Rename object key in JavaScript
- Object.create( ) In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


