How to get the last item in an object using JavaScript

1 Answer

0 votes
const obj = {
  lang1: 'javascript',
  lang2: 'c',
  lang3: 'python',
};

const lastKey = Object.keys(obj).pop();
console.log("last key: " + lastKey); 

const lastValue = Object.values(obj).pop();
console.log("last value: " + lastValue); 

console.log(obj);
   
   
   
   
/*
run:
   
"last key: lang3"
"last value: python"
{
  lang1: "javascript",
  lang2: "c",
  lang3: "python"
}
   
*/

 



answered May 11, 2022 by avibootz

Related questions

...