Harendrra
Harendrra

Reputation: 135

How to access element in array object [API]

result: [,…]

0: {id: "58cdb1a0", type: "b", nextDate: null, status: "active",…}
Date: "2019-11-27T11:10:23.000Z"
id: "58cdb1b86"
nextDate: null
status: "active"
type: "b"

1: {id: "e7b07030-799d-43", type: "l", nextDate: "2019-12-11T00:00:00.000Z",…}
Date: null
id: "e7b07030"
nextDate: "2019-12-11T00:00:00.000Z"
status: "active"
type: "l"

Then I want to access type ===b element object

const details = this.state.Method.filter((element: any) => element.type === 'b');

Then I want to get those object elements

console.log('details.nextDate');

But console getting undefined. Can you please help me to resolve it?

Upvotes: 0

Views: 47

Answers (2)

Abhisek Mishra
Abhisek Mishra

Reputation: 269

Filter will return an array. So you can read the output as

console.log(details[0].nextDate);

OR

You can use find, which will return an object.

const details = this.state.Method.find((element: any) => element.type === 'b');

Now you can use

console.log(details.nextDate);

Upvotes: 1

Boris Traljić
Boris Traljić

Reputation: 956

console.log(details[0].nextDate);

The filter() method creates a new array.

Upvotes: 1

Related Questions