How to make custom function that can access object items?

Viewed 48

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")
1 Answers

Try in the following way:

const sortArrayObj = (arrayObj, field) => {
  // How to access "arrayObj[0].field" ???
  arrayObj[0][field];
};

Related