Ryan Fonseka
Ryan Fonseka

Reputation: 255

Get a specific attribute from an object React Native

I have an object which I got from an API call. I have passed the object through route.params I want to take warrantyStatus from the tasks array for specific serialNumber which I got from route.params. I pass the serialNumber through route.params as PSerial following is my Object

      "tasks": Array [
        Object {
          "completed": false,
          "createdAt": null,
          "model": "Lexmark:T650N",
          "product": "Printer",
          "remark": "POWER SUPPLY UNIT NOT WORKING",
          "serialNumber": "794DCYL",
          "status": null,
          "topic": "21081235T69",
          "ttId": 27945,
          "warrantyStatus": "MAINTENANCE_COMPREHENSIVE",
        }, ]

I have taken the serialNumber as PSerial from route.params. So I want to take out the warrantyStatus and show in a Text tag

Upvotes: 0

Views: 273

Answers (1)

nithinpp
nithinpp

Reputation: 2025

You can filter your tasks array based on PSerial and the warrantyStatus property.

Simply find the item with,

const item = tasks.find(item => item.serialNumber === PSerial)

Now the item will have the corresponding matching object. You can access the warrantyStatus by item.warrantyStatus and show it in a Text component.

You can use the array filter function if you are expecting multiple tasks to have the same warrantyStatus property.

const items = tasks.filter(item => item.serialNumber === PSerial)

The items now will be an array having all tasks matching the provided PSerial value.

Upvotes: 1

Related Questions