1

Want to know how we can access the items in an object when passed to a function :

const sortArrayObj = (arrayObj, field) => {
  //How to access "arrayObj[0].field" ???
  // Tried to do console.log(`${arrayObj[0]}`) and it gave output as [Object object]
  //Now how can i access "name" and "date" from it using the "field" variable ?

};

const arr = [
  { name: "A", date: "13-12-1996" },
  { name: "G", date: "03-02-2020" },
  { name: "C", date: "09-05-2021" },
];
sortArrayObj(arr,"date")
ROHIT K F
  • 91
  • 3
  • 9

1 Answers1

1

Try in the following way:

const sortArrayObj = (arrayObj, field) => {
  // How to access "arrayObj[0].field" ???
  arrayObj[0][field];
};
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73