Home > OS >  get a value from an object react native
get a value from an object react native

Time:09-17

I have an object which has the following attributes. I want to show the serialNumber in task array to show in a Text element.

Data Object {
  "actionTime": null,
  "arrivedAt": null,
  "callPerson": null,
  "category": null,
  "contactNo": "0",
  "createdAt": "2021-08-06 13:27:55",
  "custName1": null,
  "custName2": null,
  "geoLocation": null,
  "id": 1,
  "location": null,
  "remark": null,
  "repeat": null,
  "tasks": Array [
    Object {
      "completed": false,
      "createdAt": null,
      "model": "Lexmark:MS510DN",
      "product": "PRINTER",
      "remark": "paper jam",
      "serialNumber": "451444HH1N1GT",
      "status": null,
      "topic": "21080295T210",
      "ttId": 27226,
      "warrantyStatus": "MAINTENANCE_COMPREHENSIVE",
    },
  ],
  "topic": null,
}

tried following but it doesn't show anything on the App.

let item = dataOBJ.tasks
console.log("PSerial :",PSerial) 

PSerial : 451444HH1N1GT

console.log("iitem :",item)

iitem : Array [ Object { "completed": false, "createdAt": null, "model": "Lexmark:MS510DN", "product": "PRINTER", "remark": "paper jam", "serialNumber": "451444HH1N1GT", "status": null, "topic": "21080295T210", "ttId": 27226, "warrantyStatus": "MAINTENANCE_COMPREHENSIVE", }, ]

const warranty = item.filter(function(item){
    return item.serialNumber == PSerial;
}).map(function({warrantyStatus}){
    return{warrantyStatus};
});

I want to show in <Text style={{ fontWeight: 'bold' }}>{item.warrantyStatus}</Text>

CodePudding user response:

In case you'll have multiple objects inside your tasks object, you could use logic from your warranty function as follows:

item.filter(function(item){
    return item.serialNumber == PSerial;
}).map((task,index) => {
   return(
      <Text
         key={index}
         style={{ fontWeight: 'bold' }}
      >{task.warrantyStatus}</Text>
)})
  • Related