JSFiddle - React, Tailwind, and code Playground

by Farzad YZ

JavaScript

function getLastFromArray(x) {
	return x[x.length - 1];
}

function getLastFromObject(x) {
	const ali = Object.keys(x);
  const lastKey = getLastFromArray(ali);
  const lastValue = x[lastKey]
  
  return {[lastKey]: lastValue}
}

/*

	getLast({
    name: "ebad",
    fname: "yz",
    age: 21
  });
  
  {age: 21}
  
*/

console.log(getLastFromObject({
  name: "ebad",
  fname: "yz",
  age: 21
}))

console.log(getLastFromObject({
	id: 1,
  num: 2
}))