OneCompiler

[Javascript] How to get an element present in second object of array of Objects in javascript

I have an array of objects like below. I want to retrive the second author William Shakespeare from the below array? Is there any simple way with out writing loops?

const authors = [
  {
    "author": "Chetan Bhagat",
    "id": "121"
  },
  {
    "author": "William Shakespeare",
    "id": "122"
  },
  {
    "author": "J.K. Rowling",
    "id": "123"
  },
  { "author": "William Faulkner", 
    "id": "124" 
  }
];

2 Answers

4 years ago by

You can use object destructuring like below to retrieve the second author alone.

let [,{author}]=authors;
console.log(author);

Try checking the result https://onecompiler.com/javascript/3xme8sfuf

4 years ago by Meera

You can just straigt forward use
authors[1].author

4 years ago by Swat Boi