Jane
Jane

Reputation: 21

Javascript Count array of object base on array value and use it on react native

this code works perfectly on my browser but when i applied it on react native i have an error of

" Possible Unhandled Promise Rejection (id: 0): TypeError: obj.room_type_id.includes is not a function. (In 'obj.room_type_id.includes(id)', 'obj.room_type_id.includes' is undefined) "

var data = [{
  "floor": "1st",
  "name": "NAME0",
  "room_type_id": ["Y", "B", "S", "N"],
  "status": "Available"
}, {
  "floor": "7",
  "name": "NAME54",
  "room_type_id": ["O", "G", "C", "S"],
  "status": "Available"
}, {
  "floor": "64",
  "name": "NAME2",
  "room_type_id": ["A", "S", "Q", "D"],
  "status": "Available"
}, {
  "floor": "Bddh",
  "name": "NAME3",
  "room_type_id": ["A", "X", "S", "D"],
  "status": "Available"
}, {
  "floor": "Vh",
  "name": "NAME1",
  "room_type_id": ["A", "B", "C", "D"],
  "status": "Available"
}, {
  "floor": "Dyd",
  "name": "NAME2",
  "room_type_id": ["A", "B", "C", "D"],
  "status": "Available"
}]

let id = "B";
let dataResult = data.filter((obj) => obj.room_type_id.includes(id)).length;


console.log(dataResult)

Upvotes: 1

Views: 224

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

Have a try by parsing the array object to the JSON object to use the array itteration methods.

let JSONData = JSON.parse(JSON.stringify(data))
let dataResult = JSONData.filter((obj) => obj.room_type_id.includes(id)).length;

Upvotes: 1

Related Questions